开发者

Attached objects vs Detached objects in entity framework

开发者 https://www.devze.com 2023-02-22 21:51 出处:网络
What are the benefits of using attached objects vs detached object开发者_如何学JAVAs? What I\'m currently doing in my repository is manually detach my objects before i update or delete them. So if I\

What are the benefits of using attached objects vs detached object开发者_如何学JAVAs?

What I'm currently doing in my repository is manually detach my objects before i update or delete them. So if I'm updating or deleting, I do not do a round trip but I delete by ID. I think working with detached scenario works for me. Am I doing something wrong?

I'm working with an n-teir app that utilizes asp.net mvc and wcf.


Using attached objects will allow you to manipulate, track changes, do concurrency optimization. In most cases I use attached objects for updates or in a stateful application. This will also allow you to have lazy-loading and to benifit from the context cache. If you are using entity framework in a statefull manner, this is great because you can reduce the number of calls to the database when you require a single object from the context. Using the GetObjectByKey will query the context before making a query to the database. if the object was previously loaded, it will save you a round trip to the database.

Using detached objects are great! it allows for faster reads, simpler objects to materialize, a smaller memory footprint for the entity context. It is also best when sending data over the wire (wcf.. services). Anything out of scope, or even when you are converting the objects to domain objects. Since you do not require any tracking on the objects, this is a good optimization to start with. This can be acheived quickly using the NoTracking merge option on the entity set.

Detached objects will also greatly simplify working with EF in environments where you have many instances of your context. Simply attach the object before making changes and saving.

Note: using NoTracking will not allow you to use lazy-loading, change tracking, GetObjectByKey or any of the statefull functionalities of entity framework. Using NoTracking you will need to use eager loading ("Include()") to load related entities / navigation properties. EntityKeys will also not be loaded.

Edit:

Lazy-loading on detached entities, does not work because it does not have a context to refer to for its queries. The entity may also be missing the required proxies and entitykeys.

I would greatly suggest using eager loading. This may also be an optimisation in the end because it is hard to guage the impact of lazy-loading. Because it can produce situations where if you are iterating a collection, it will make a request to the database for each object in the collection. This can be quite problematic when you have large collections.


entity.User.Attach(model);
entity.ObjectStateManager.ChangeObjectState(model,System.Data.EntityState.Modified);
entity.SaveChanges();
return View(model);
0

精彩评论

暂无评论...
验证码 换一张
取 消