Sub entity doesn't update when assign null value to its reference

左心房为你撑大大i 提交于 2019-12-11 04:45:04

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/7256083/sub-entity-doesnt-update-when-assign-null-value-to-its-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!