简体   繁体   English

EF6事务错误在ObjectContext中

[英]EF6 transaction error in ObjectContext

please can someone help i'm trying to use Transaction in EF6. 请有人可以帮助我尝试在EF6中使用事务。 it's my first try in transaction in .net. 这是我第一次在.net中进行交易。 i have read the manual by Microsoft in their site. 我已经在他们的网站上阅读了Microsoft的手册。 http://msdn.microsoft.com/en-us/data/dn456843.aspx and i would to use a transaction for user registration but nothing is working. http://msdn.microsoft.com/zh-cn/data/dn456843.aspx ,我将使用事务进行用户注册,但是没有任何效果。 Im using EF objectContext . 我正在使用EF objectContext here is a little piece of my code : 这是我的代码的一小段:

using (var context= new MyModelContainer())
{ 
 using(var dbContextTransaction = context.Database.BeginTransaction())
      {
       try 
         {
          // code here to create user
          context.savechanges();
          // code here to add role
         context.savechanges();

         dbContextTransaction.Commit(); 
        }
      catch 
         {
         dbContextTransaction.Rollback(); 
         }
     }
}

My problem is that Visual Studio does not even recognize Database.BeginTransaction() . 我的问题是Visual Studio甚至无法识别Database.BeginTransaction() may be because we are using objectContext ? 可能是因为我们正在使用objectContext吗? i never use transaction. 我从不使用交易。 i change to use another database where our model is DbContext it's seems to work. 我更改为使用另一个我们的模型为DbContext数据库,它似乎正常工作。

How can we use objectContext with transaction also (mean not distributed systems) ? 我们如何还可以将objectContext与事务一起使用(意味着不是分布式系统)? any tutorial please ? 有任何教程吗?

I tried transaction scope but it's seem to work but i read that it's for distributed system ((( it's means that performance going down. any suggestion ? thanks for your time !!! 我尝试了事务范围,但它似乎可以工作,但是我读到它适用于分布式系统(((这意味着性能下降。有什么建议吗?感谢您的时间!

Replace the line 更换线

using(var dbContextTransaction = context.Database.BeginTransaction())

with this: 有了这个:

using(var dbContextTransaction = new TransactionScope())

and remove the call to Rollback. 并删除对回滚的调用。 TransactionScore will do what you need. TransactionScore将满足您的需求。

TransactionScope is compatible with distributed systems, but if you do not make a call to other db's and use it as described, then it is more than good enough for your needs. TransactionScope与分布式系统兼容,但是如果您不调用其他数据库并按说明使用它,则它足以满足您的需求。 If you try to make a call to save to another db (for example) then it will require special privileges and an exception will be thrown. 如果您尝试调用保存到另一个数据库(例如),则它将需要特殊的特权,并且将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM