简体   繁体   English

c#实体框架抛出异常但没有回滚数据库

[英]c# entity framework throw exception but no roll back on database

I have a simple query which updates two tables in my database 我有一个简单的查询,它更新了我的数据库中的两个表

public void UpdateLogins(int userId)
{
    using (var context = new storemanagerEntities())
    {
        user item = context.users.Where(x => x.id == userId).FirstOfDefault();
        item.logins += 1;
        context.SaveChanges();

        account accountItem = context.accounts.Where(x => x.userId == userId).FirstOfDefault();
        accountItem.logins += 1;
        context.SaveChanges();
    }
}

but if I but a "throw new Exception();" 但如果我只是“抛出新的异常();” between them like so 他们之间是这样的

        context.SaveChanges();

        throw new Exception();

        account accountItem = context.accounts.Where(x => x.userId == userId).FirstOfDefault();

the user table is not updated but my account table has not been updated, and the database saves these changes. 用户表未更新,但我的帐户表尚未更新,数据库保存这些更改。 How can I tell my database to rollback the changes if a exception is thrown? 如果抛出异常,如何告诉我的数据库回滚更改?

Thanks 谢谢

Try this : 试试这个 :

using (TransactionScope txScope = new TransactionScope())
{    
    using (var context = new storemanagerEntities())
    {
        user item = context.users.Where(x => x.id == userId).FirstOfDefault();
        item.logins += 1;
        context.SaveChanges();

        account accountItem = context.accounts.Where(x => x.userId ==     userId).FirstOfDefault();
        accountItem.logins += 1;
        context.SaveChanges();
     }
     txScope.Complete();
 }

Each .SaveChanges is a transaction. 每个.SaveChanges都是一个交易。 So you have 2. 所以你有2个。

As others pointed out enclose all in parent TransactionScope and then these 2 will became child transactions. 正如其他人所指出的那样,将所有内容包含在父TransactionScope中,然后这些将成为子事务。

Ie you'll have 1 transaction. 即你将有1笔交易。

Or better to make just one call to .SaveChanges() 或者更好地只调用一次.SaveChanges()

When operating with your models you should use C# references to connect models, not by ids. 使用模型时,应使用C#引用来连接模型,而不是使用ID。 Sometimes that is difficult, but that is the goal of ORMs, to let you work in OO terms. 有时这很困难,但这是ORM的目标,让你以OO术语工作。

User can have an Account property and that is a connection in OO terms, not by ID. 用户可以拥有一个帐户属性,这是一个OO术语的连接,而不是ID。 Same for Account - User property. 帐户 - 用户属性相同。

Order instance can have List OrderDetails property. 订单实例可以具有List OrderDetails属性。

Look here for nice examples: EF Code First Cookbook — Recipe #4: Adding Details to a Master Entity 看看这里有很好的例子: EF Code First Cookbook - 食谱#4:向主实体添加细节

You could try this: 你可以试试这个:

http://social.msdn.microsoft.com/Forums/en-US/adeb3cf3-db49-40d2-99ae-c14b66fef1e3/rollback-and-commit-using-entity-framework?forum=adodotnetentityframework http://social.msdn.microsoft.com/Forums/en-US/adeb3cf3-db49-40d2-99ae-c14b66fef1e3/rollback-and-commit-using-entity-framework?forum=adodotnetentityframework

The data is going to be saved to the database but not committed until the Scope.Complete() is done. 数据将保存到数据库,但在Scope.Complete()完成之前不会提交。

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

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