我使用EntityFrameworkCore.SQLite v1.1.3,VS 2015,制作简单的WPF应用程序,.Net 4.6
People
和Orders
表与#34;多对多相关"通过我的数据库中的OrdersToPeople
表。我使用SQLite Tools制作了dbContext类
我使用此代码检查加载的数据:
var list = myDbContext.People
.Include(t => t.OrdersToPeople);
foreach (var element in list)
{
var c = element.OrdersToPeople.Count;
//c is always 0. Why?
}
当我以相同的方式加载OrdersToPeople
或Orders
个表时
var list = myDbContext.OrdersToPeople
或
var list = myDbContext.Orders
,我看到了数据。当我进行SQL查询时,它返回正确的数据。
为什么Include
无法加载OrdersToPeople
?
P.S。 OrdersToPeople
字段为virtual
。
public partial class People
{
//...fields...
public virtual ICollection<OrdersToPeople> OrdersToPeople { get; set; }
public People()
{
OrdersToPeople = new HashSet<OrdersToPeople>();
}
}
public partial class OrdersToPeople
{
public long Id{ get; set; }
public long PeopleId { get; set; }
public long OrderId { get; set; }
public virtual People People { get; set; }
public virtual Orders Orders { get; set; }
}
答案 0 :(得分:0)
我认为这种行为是可以预期的。您是否尝试在选择中调用ToList?
看起来EF在没有ToList的情况下忽略了包含: Entity Framework Core ignoring .Include(..) without .ToList(..) indirectly
var list = myDbContext.People
.Include(t => t.OrdersToPeople);
.ToList();