DbContext.Entry附加实体 [英] DbContext.Entry attaching Entity

查看:180
本文介绍了DbContext.Entry附加实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的研究中,我读到调用DbContext.Entry(someEntity)会自动将实体附加到上下文。



然而,当我这样做时,我发现实体的状态是分离的。



任何人都可以看出这一点,以及DbContect.Entry如何工作。我使用EF 5.0



谢谢。

解决方案

如果你想要附加一个对象,你实际想要的是 DbSet.Attach 。 DbContext.Entry只提供有关实体的信息,并允许您更改状态(如果已经附加)。



这是一个关于MSDN的实体状态的好消息


From my research, I read that calling DbContext.Entry(someEntity) would automatically attached the entity to the context.

However, when I do this I find that the entity's state is detached.

Can anyone shed some light on this and how the DbContect.Entry works. I'm using EF 5.0

Thanks.

解决方案

If you're wanting to attach an object, what you actually want is DbSet.Attach. DbContext.Entry is only giving you information about the entity, and allows you to change the state if it's already been attached.

Here's a good post about entity states from MSDN

这篇关于DbContext.Entry附加实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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