“由上下文跟踪的实体”是什么意味着实体框架 [英] What does "Entity Tracked by Context" mean in Entity Framework

查看:101
本文介绍了“由上下文跟踪的实体”是什么意味着实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个EF Team Blog的这个系列 http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature -ctp5-part-1-introduction-and-model.aspx

I am reading this E.F. Team Blog's this series http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx

在许多地方,我看到这个术语实体跟踪,特别是在这个部分 http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4- add-attach-and-entity-states.aspx (part4)

At many places i saw this term "Entity Tracked by Context", specially in this part http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx (part4)

我的问题是


  1. 上下文跟踪的实体是什么意思?

  1. What does "Entity Tracked by Context" mean??

相同的上下文用于每个请求或新的为每个请求创建上下文(我正在使用EF for Asp.Net MVC APP)

Does same context is used for every request or new context is created for each request (I am using E.F. for Asp.Net MVC APP)

跟踪e非常实体(加载)必须在服务器上添加一些内存开销,是吗?

Tracking every entity (loaded) must be adding some memory-overhead on server, is it??


推荐答案

上下文跟踪的实体意味着上下文知道实体,它知道实体的状态和对实体所做的更改。上下文只能跟踪实体。如果调用保存更改,则只跟踪实体上的更改将保留到数据库。跟踪和附加可以被认为是同义词。

"Entity Tracked by Context" mean that context is aware of the entity, it knows state of the entity and changes made to the entity. Context can work only with tracked entities. If you call save changes only changes on tracked entities will be persisted to the database. Tracked and Attached can be considered as synonyms.

在EF中,我们通常会谈论附加的实体和分离的实体。上下文跟踪附加实体。如果您从数据库加载实体,除非您手动为EF追踪实体,或者如果您调用附加添加(DbContext API)/ AddObject (ObjectContext API)为实体。您可以通过调用 Detach (ObjectContext API)或将状态设置为 Detached ( DbContext API)。如果您只是在代码中创建任何其他类的实体,则将其视为已分离,直到您调用附加为止。

In EF we are usually talking about attached entities and detached entities. Attached entities are tracked by the context. Entity become attached if you load it from database (unless you manually for EF to don't track the entity) or if you call Attach or Add (DbContext API) / AddObject (ObjectContext API) for the entity. You can force entity to detach from the context either by calling Detach (ObjectContext API) or setting the state to Detached (DbContext API). If you just create the entity in your code as any other class it is considered as detached until you call Attach for it.

每个请求总是创建新的上下文 - Web应用程序与分离的实体工作很多。这也将解决内存问题。如果您分离所有要存储在某些状态(如会话)的实体,并且如果您在每个请求中正确处理上下文,您将释放内存。

New context is always created for each request - web application works a lot with detached entities. That will also solve the issue with memory. If you detach all entities you want to store in some state (like session) and if you dispose the context correctly in each request you will free the memory.

这篇关于“由上下文跟踪的实体”是什么意味着实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆