问题描述
我在使用Entity Framework 4.1 Code First的ASP.NET MVC 3应用程序中遇到了一个有趣的错误。我有三个类/表依次连接。有一个邀请
引用了一个项目
,然后引用公司
。 当我加载公司并保存一切都很好。项目相同然而,当邀请被编辑和保存时,它会清除公司中的字段。他们都只是空白!
当我编辑项目时,我需要显示公司的一些信息,所以我明确地加载了 .Include(x => x.Company)
。当我编辑邀请,我不需要公司,所以我没有打扰包括它。
我会认为,如果对象不是那么EF应该不会有什么理由把它标记为已编辑的,对吗?
中查找
内部的更改检测,认为 MainContact
在公司
中作为新实体,并将其放入中添加
状态上下文。
I'm running into an interesting bug in my ASP.NET MVC 3 application using Entity Framework 4.1 Code First. I have three classes/tables that are joined in sequence. There's an Invitation
that has a reference to a Project
which then has a reference to Company
.
When I load a company and save it everything is fine. Same for projects. However when the invitation gets edited and saved, it wipes out the fields in the company. They're all just blank!
When I edit the project, I need to show some info from the company, so I'm explicitly loading that with .Include(x => x.Company)
. When I edit the invitation though, I don't need the company so I haven't bothered including it.
I'd think that if the object wasn't ever loaded, then there shouldn't be any reason for EF to flag it as edited, right?
Find
which thinks to identify the MainContact
in company
as a new entity and puts it into Added
state into the context.
这篇关于什么会导致实体框架保存一个卸载(但可延迟的可加载)参考现有的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!