[英]How do you check the state of a POCO that is attached to the EF Context?
What is the way to check the State of an entity which is a POCO (not derived from EntityObject) and that is attached to the EF context?检查作为 POCO(不是从 EntityObject 派生的)并附加到 EF 上下文的实体的状态的方法是什么?
Thanks!谢谢!
If you are using the DbContext and c is your entity reference如果您使用的是 DbContext 并且 c 是您的实体引用
var state = Context.Entry(c).State;
if you are using ObjectContext.如果您使用的是 ObjectContext。
//if x is your entity reference
var state = context.ObjectStateManager.GetObjectStateEntry(x);
if you are employing an identifier field and using DBContext
, you may check using the following, assuming that the Id
of entity is entityId;如果您使用标识符字段并使用
DBContext
,则可以使用以下内容进行检查,假设实体的Id
是 entityId;
if(Context.Entities.Local.Any(q => q.Id == entityId))
{
// already attached to the context
}
The Local property of DbSet provides simple access to the entities of the set that are currently being tracked by the context and have not been marked as Deleted.
DbSet 的 Local 属性提供对当前正在由上下文跟踪且尚未标记为已删除的集合实体的简单访问。 Accessing the Local property never causes a query to be sent to the database.
访问 Local 属性永远不会导致向数据库发送查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.