Linq 实体框架模型导航已中断
我正在使用EF,存储库模式和服务来访问数据,我的数据出现了一些奇怪的情况:Linq 实体框架模型导航已中断,linq,entity-framework,code-first,Linq,Entity Framework,Code First,我正在使用EF,存储库模式和服务来访问数据,我的数据出现了一些奇怪的情况: public interface IEntity { int ID { get; set; } } public partial class Page : IEntity { public ICollection<Post> Posts { get; set; } public UserProfile User { get; set; } int _id; [K
public interface IEntity {
int ID { get; set; }
}
public partial class Page : IEntity {
public ICollection<Post> Posts { get; set; }
public UserProfile User { get; set; }
int _id;
[Key]
public int ID {
get { return _id; }
set { _id = value; }
}
}
public partial class Post : IEntity {
public Page Page { get; set; }
int _id;
[Key]
public int ID {
get { return _id; }
set { _id = value; }
}
}
我不再这样做了,因为p.Page=null,即使数据库中存在数据。如果我这样做:select p.Page.Count()
——我会得到正确的计数,但是如果我这样做:select p.Page
,什么都没有
我不明白如何发现/修复出错的地方?如果在调试模式下运行应用程序,是否会发生异常?不,没有异常。控制台中甚至没有第一次出现异常。页面属性中是否有虚拟机丢失或Fluent API不再声明外键的情况?页面实体的
用户导航属性在哪里?您似乎是先使用DB,然后再使用DbContext generator来首先编写代码?您只见过/尝试过使用虚拟、nav属性。不知道为什么它曾经对你有用。有没有可能数据当时就在上下文中?
var posts = (from p in postDb.GetAll()
where p.Page.User.ID == userId && p.Status == Status.Pending
select p);