简体   繁体   中英

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. So you have 2.

As others pointed out enclose all in parent TransactionScope and then these 2 will became child transactions.

Ie you'll have 1 transaction.

Or better to make just one call to .SaveChanges()

When operating with your models you should use C# references to connect models, not by ids. Sometimes that is difficult, but that is the goal of ORMs, to let you work in OO terms.

User can have an Account property and that is a connection in OO terms, not by ID. Same for Account - User property.

Order instance can have List OrderDetails property.

Look here for nice examples: EF Code First Cookbook — Recipe #4: Adding Details to a Master Entity

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

The data is going to be saved to the database but not committed until the Scope.Complete() is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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