How do I detach objects in Entity Framework Code First?

泪湿孤枕 提交于 2019-12-17 03:52:33

问题


There is no Detach(object entity) on the DbContext.

Do I have the ability to detach objects on EF code first?


回答1:


If you want to detach existing object follow @Slauma's advice. If you want to load objects without tracking changes use:

var data = context.MyEntities.AsNoTracking().Where(...).ToList();

As mentioned in comment this will not completely detach entities. They are still attached and lazy loading works but entities are not tracked. This should be used for example if you want to load entity only to read data and you don't plan to modify them.




回答2:


This is an option:

dbContext.Entry(entity).State = EntityState.Detached;


来源:https://stackoverflow.com/questions/5599147/how-do-i-detach-objects-in-entity-framework-code-first

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