Entity to Linq AcceptAllChanges SaveChanges

c# entity entity-framework


What makes the following two different:

        // vs

        // vs

and the reason whydb.Customers.DeleteObject(Mycustomer); and nodb.DeleteFromCustomer(Mycustomer);

Which one should I use when?

Entity Framework is threadsafe, too. Would it crash if two threads updated the context's object at the same time?

I appreciate you.

2/21/2011 8:31:34 AM

Accepted Answer

db.AcceptAllChanges() thinks you are done with any related change history and discards it; if you have any more issues, you will be unable to restore those changes.db.SaveChanges(false) does preserve a record of those adjustments in case something goes wrong.

Zzz-13-Zzz has a more thorough response; see it.

db.AddToCustomer() is a wrapper around highly typed code.db.Customers.AddObject() . You can understand what I mean if you look at the definition of it. I'd employ thedb.AddToCustomer() method just because it is compile-time type checked and highly typed.

I suppose the only explanation for the absence ofDeleteFromCustomer() that they believed the job wouldn't be required (people tend to add more than they delete). However, there is nothing stopping you from implementing it yourself by developing your own extension methods.

You must handle the locking yourself if you wish to do updates since the EF is not thread safe. To learn more, go to http://blog.cincura.net/230902-multithreading-with-entity-framework/.

2/21/2011 8:44:18 AM

Popular Answer

AcceptAllChanges only includes all newly added and altered entities inObjectContextStateManager example toUnchanged All removed entities were set to state and detached, however the database modifications were not carried out.SaveChanges performs database changes and, by default, accepts modifications (can be configured not to do it).

AddToCustomer is equivalent toCustomers.AddObject - It is only a shortcut (similar toDeleteObject ). The first approach is produced by a code generator (and I believe it refers to the second approach as the typical approach toObjectSet ).

Thread safety is a weakness of entity framework. Additionally, you need to be extremely cautious while sharing.ObjectContext amongst several threads.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow