重新加载导航属性EF [英] Reload navigation property EF
本文介绍了重新加载导航属性EF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将EF6与DB-First一起使用.我的数据库包含2个表:Authors和Books.使用EF加载对象后,我直接删除了数据库中的行(例如在SQL Manegment Studio中或使用ExecuteNonQuery()),如何重新加载导航属性?在Load()之后,我可以在myObj中看到相同的记录Books.如果我直接插入数据库,则在myObj上的Load()之后可以看到新的记录Books.
I use EF6 with DB-First. My DB contains 2 tables: Authors and Books. After load object with EF I delete row in DataBase directly (in SQL Manegment studio for example or use ExecuteNonQuery()), how to reload navigation property? I get to see the same records Books in myObj after Load(). If I insert directly into the database, the new records Books are visible after Load() on myObj.
var cnt = new Entities();
var myObj = cnt.Authors.
Where(x => x.Name = "Author Name").
Include("Books").
FirstOrDefault();
// Delete directly in database (ex: Delete From Books Where ID = ...)
// DeleteDirect();
cnt.Entry(myObj).Collection("Books").Load();
推荐答案
使用DataContext的Refresh方法刷新数据的本地副本:
Use the Refresh method of DataContext, to refresh the local copy of data:
DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues,DataContext.Orders);
这篇关于重新加载导航属性EF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文