If you are using Entity Framework and closing or disposing of your DataContexts (read database connections) as soon as your Linq to Entities query have finished, which I believe you should, you will run into the situation where saving changes to an updated entity will be ineffectual.
I've discussed before that I think deliberately keeping database connections open is bad in Entity Framework but when practicing this technique you will find saving changes to an updated entity no longer works as it used to when the DataContext was left open as long as it liked.
This is because when you open a DataContext you are also starting up change tracking. Change tracking will keep a note of if your entity has been changed. This is important when you call SaveChanges on that DataContext because it needs to know if there have been any changes to save.
If, however, you open your DataContext, do you Linq to Entities query to get an entity and then dispose of your DataContext your entity is now detached. To SaveChanges you will need to open an new DataContext, which will have no idea that this entity has been changed. So you need a way to update a detached Entity.
Update a detached entity with your DataContext
You can do this by using the Attach method to attach this entities to the DataContext (so it is now managing it). After you've attached it it will be in the "Unchanged" state so will not do anything on SaveChanges. You can tell it it has been updated by changing its object state to Modified. Then simply save changes.
Here is an example. (This is not my actual code - it is more explicit to aid understanding):
public void UpdateObject(T obj) { using (var context = new MyDataContext()) { context.CreateObjectSet<T>().Attach(obj); context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified); context.SaveChanges(); } }
Updating a detached model when using EF Code First
Coming back to my blog years later (Feb 2013) to reuse the above code I found it didn't work in Entity Framework Code First Model. This is because the context mentioned above is a different from the DbContext used in Code First and so has different properties and methods. Here is the code I used to make this work in EF Code First:
public void UpdateObject(T obj) { using (var context = new MyDataContext()) { context.Set<T>().Attach(obj); context.ChangeTracker.Entries<T>().First(e => e.Entity == obj) .State = EntityState.Modified; context.SaveChanges(); } }
Thank you, this works like a charm and best of all I understand what i'm doing with all the guff involved in Entity framework to account for.
ReplyDeleteMotifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan, Summer collection. Shipping worldwide. Stitching option available.
ReplyDeleteNice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this!
ReplyDeleteVisit us for suction coffee mugs.
Thank you, this works like a charm and best of all I understand what i'm doing with all the guff involved in Entity framework to account for.
ReplyDeleteدانلود آهنگ های پرطرفدار جدید
دانلود آهنگ شاد
Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan, Summer collection. Shipping worldwide. Stitching option available. leicaarchive
ReplyDeleteDo your grocery at Al-Fatah, Pakistan's best and largest online grocery store in lahore website. Shop from a wide range of grocery including oil and ghee, tea and coffee, dry fruits and seeds, dairy and fresh bakery items, packaged foods, snacks and beverages, baby care, household essentials, fruits and vegetables and many more.
ReplyDeleteShop at Al-Fatah for original branded ,hair straighteners, curlers, crimpers and dryers in Pakistan at special price. Shop from our Imported Variety of Hair Straighteners, Hair Curlers, Hair Stylers, Flat Irons and Hair Styling tools at Al-Fatah. Shop from all the top brands of Hair Styling Tools at Al-Fatah today.
ReplyDeleteDo you need Quick Loan to settle your bills and startup
ReplyDeletebusiness? contact us now with your details to get a good
Loan at a low rate of 2% per Annual
Do you need Personal Finance?
Business Cash Finance?
Unsecured Finance
Fast and Simple Finance?
Quick Application Process?
Finance. Services Rendered include,
*Debt Consolidation Finance
*Business Finance Services
*Personal Finance services Help
contact us for more information on how to get started:
(Whats App) number:+919394133968
please contact email id : patialalegitimate515@gmail.com
Mr Sorina Jeffery
thanks for share prefect
ReplyDeleteجدیدترین آهنگها