Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF更新事务中的FormView_C#_Asp.net_Entity Framework_Transactions - Fatal编程技术网

C# EF更新事务中的FormView

C# EF更新事务中的FormView,c#,asp.net,entity-framework,transactions,C#,Asp.net,Entity Framework,Transactions,我想在一个事务中更新两个FormView。如果其中一个失败,另一个也应该失败。FormView有自己的实体数据源 button1_click(..........) { formview1.updateItem(true); formview2.updateItem(true); } 好吧,这可能不是世界上最简单的事情 基本的答案是:是的,你能做到。如果updateItem方法打开数据库连接,代码将类似于此 using (TransactionScope scope = new T

我想在一个事务中更新两个FormView。如果其中一个失败,另一个也应该失败。FormView有自己的实体数据源

button1_click(..........)
{
   formview1.updateItem(true);
   formview2.updateItem(true);
}

好吧,这可能不是世界上最简单的事情

基本的答案是:是的,你能做到。如果updateItem方法打开数据库连接,代码将类似于此

using (TransactionScope scope = new TransactionScope())
{
    formview1.updateItem(true);
    formview2.updateItem(true);
    scope.Complete();
}
另一方面,如果在调用updateItem时连接已经打开,那么您将需要执行以下操作

using (TransactionScope scope = new TransactionScope())
{
    formview1.Connection.EnlistTransaction(Transcation.Current);
    formview2.Connection.EnlistTransaction(Transcation.Current);
    formview1.updateItem(true);
    formview2.updateItem(true);
    scope.Complete();
}