在我发现在 EF Code first 中插入操作后导航属性没有填充后,我决定创建一个方法来重新加载导航。假设我们有两个类:
public class Book
{
public int Id {get ; set;}
public string Title {get ; set;}
public virtual ICollection<Page> Pages {get ; set;}
}
public class Page
{
public int Id {get ; set;}
public string Content {get ; set;}
public int BookId {get ; set;}
public virtual Book Book {get ; set;}
}
在下面的代码中我有:
DbContext db = new DbContext()
Page p = new Page();
p.BookId = 1;
p.Content = "Sample Content";
db.Pages.Add(p);
db.SaveChanges();
db.Reload(p, rec => rec.Book); // Here is the wanted method
我试过这个,但我失败了
public class DbContext : DbContext
{
///I know this method is wrong, I have no idea to fix it
public T Reload<T>(T Obj, param Func<T,I_DONT_KNOW_WHAT_I_SHOULD_PUT_HERE>[] predicates )
{
foreach(var item in predicates)
{
this.Entry<T>(Obj).Reference(item).Load();
}
}
}
有没有人可以帮我完成这个方法?