c#中带有db上下文的事务

本文关键字:上下文 事务 db | 更新日期: 2023-09-27 18:05:07

我正在尝试从c#上下文提交事务:

private TestDbEntities context =new TestDbEntities(ConnectionString); 
this.context.Connection.Open();
System.Data.Common.DbTransaction transaction = this.context.Connection.BeginTransaction();
DbCommand command = this.context.Connection.CreateCommand();
command.CommandText = "Some Insert/Update Query";
command.ExecuteNonQuery();
transaction.Commit();

但是我得到错误信息:

"查询语法无效。在标识符Table_Name附近,第1行列8。"

查询似乎很好,当我直接执行直接在sql管理工作室。有解决办法吗?

c#中带有db上下文的事务

 SqlConnection conn = new SqlConnection(); 
 SqlCommand command1 = new SqlCommand(); 
 SqlCommand command2 = new SqlCommand(); 
            SqlTransaction trans = conn.BeginTransaction();
            command1.Transaction = trans;
            command2.Transaction = trans;
            try
            {
                command1.ExecuteNonQuery();
                command2.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
            }
            finally
            {
            }