我在代码优先模型中使用EF6,当我查询一对多关系的“多”方面时,我遇到了引入急切加载的导航属性的问题。我已经尝试将.Include添加到.Where,但它会抛出错误。
public partial class OneSide
{
public OneSide()
{
Configurations = new Collection<ManySide>();
}
...
// navigation properties
public ICollection<ManySide> Configurations { get; set; }
}
public partial class ManySide
{
...
public int IND2OBJECT { get; set; }
// navigation properties
public OneSide Listing { get; set; }
}
在OnModelCreating:
modelBuilder.Entity<ManySide>()
.HasRequired<OneSide>(s => s.Listing)
.WithMany(s => s.Configurations).HasForeignKey(s => s.IND2OBJECT);
这就是我要查询的内容 - 我想在.Where之后添加.Include:
using System.Data.Entity;
IQueryable<ManySide> allRecs = DbContext.ManySides;
IQueryable<ManySide> dayRecs =
allRecs.Where(
x => (string.Compare("2014", x.begin) >= 0) &&
(string.Compare("2015", x.end) <= 0)).Include( b => b.Listing);
foreach (var dayRec in dayRecs) // <-- error happens here
<do something with dayRec>
我得到的错误是:
A specified Include path is not valid. The EntityType 'ManySide' does not
declare a navigation property with the name 'Listing'.
但是我在POCO和OnModelCreating中定义了导航属性我在Fluent API中正确定义了一对多关系,不是吗?我也尝试过不使用DBExtentions(使用.Include(“Listing”)),但结果相同。
如果我完全删除.Include,我可以获得ManySide记录,但'Listing'为空。
感谢您的任何建议! 科里。