Using TransactionScope with Entity Framework 6

后端 未结 2 448
余生分开走
余生分开走 2020-12-24 07:29

What I can\'t understand is if its possible to make changes to the context and get the changes in the same transaction before its commited.

This is what I´m looking

2条回答
  •  佛祖请我去吃肉
    2020-12-24 07:54

    If you want to make sure that you only query the local content of your context you can use the "local" collection:

    Item thisItem = context.Items.First();  
    thisItem.Name = "Update name";    
    Item thisUpdatedItem = context.Items.Local.Where(a=>a.Name == "Update name").First();
    

    This will only query the in-memory data of the context and will not hit the database.
    The "Local" data is present as soon as you materialize an object in the context by adding it or loading it from the database, i.e. you do not need to call SaveChanges().
    SaveChanges() will write the content of the context to your database.

提交回复
热议问题