问题描述
我有一个名为:应用程序的实体,它的实体也是实体的ApplicationStatus ,ApplicationStatus值为yes或no,在Application中为空。所以当我执行:
应用程序应用程序= MyEntities.Applications 。第一();
application.ApplicationStatus = null;
MyEntities.SaveChanges();
ApplicationStatus不会更新其值,除非我调试它,然后执行快速查看应用程序.ApplicationStatus。
然而
application.ApplicationStatusId =空值;
工作正常。
为什么这个不起作用:
application.ApplicationStatus = null;
感谢
编辑 / p>
由于@LadislavMrnka答案解决了调试行为,并且与懒惰加载相关的问题:
我们如何解释这些情况?
应用程序应用程序= MyEntities.Applications.First();
application.ApplicationStatusId = null; //此值更新为db
ApplicationStatus applicationStatus = MyEntities.ApplicationStatuses.First(a => a.Name ==no);
app.ApplicationStatus = applicationStatus; //这更新db中的值
app.ApplicationStatus = null; //但是这不是
将工作:
应用程序应用程序= MyEntities.Applications.Include(ApplicationStatus)。
原因是您的 ApplicationStatus
从未加载。如果您调试和访问它,您的调试访问将触发延迟加载并加载它。
I have an entity named: Application and it has field ApplicationStatus which is entity as well, ApplicationStatus values are yes or no and it's nullable in Application.
So when I execute:
Application application = MyEntities.Applications.First();
application.ApplicationStatus = null;
MyEntities.SaveChanges();
ApplicationStatus won't update its value, unless I debug it and do quick watch application.ApplicationStatus.
However
application.ApplicationStatusId = null;
works fine.
My question, why this one doesn't work:
application.ApplicationStatus = null;
Thanks
EDIT further question:
As @LadislavMrnka answer solves debug behaviour and and the issue related lazy loading:
how can we interpret these situation?
Application application = MyEntities.Applications.First();
application.ApplicationStatusId = null; //This updates value in db
ApplicationStatus applicationStatus = MyEntities.ApplicationStatuses.First(a => a.Name == "no");
app.ApplicationStatus = applicationStatus; //This updates value in db too
app.ApplicationStatus = null; // But this doesn't
Use this and it will work:
Application application = MyEntities.Applications.Include("ApplicationStatus").First();
The reason is that your ApplicationStatus
was never loaded. If you do debug and access it your debugging access will trigger lazy loading and loads it.
这篇关于当分配null值给它的引用时,子实体不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!