我的样本数据组织如下:
每个Custumer
有多个Location
,每个Location
拥有一个 Address
。此模式在模型创建时组织为:
modelBuilder.Entity<Customers>().HasMany(c => c.Location)...
modelBuilder.Entity<Locations>().OwnsOne(l => l.Address)...
当我查询Location
时,Location
会自动包含Address
(按预期方式)。
var location = myContext.Locations.First(); // Address is populated
换句话说,我不需要在查询中添加.Include(c => c.Address)
。
然而,当我的查询开始查询Costumer
var c = myContext.Customers.Include(c => c.Location); // Address is not populated
Locations
不包含Address
。然后我必须在查询中添加.ThenInclude(c => c.Address)
。这不是预期的。然后我需要写下来......
var c = myContext.Customers.Include(c => c.Location).ThenInclude(c => c.Address);
这是预期的行为还是我错过了什么?