我试图将注意力集中在EF Cores拥有的对象上,以及如何控制何时加载某些数据块。
基本上,我有一堆旧的旧表(有些具有约150列),并希望使用根实体和每个表几个拥有的对象对它们进行建模,以实现更好的细分并捆绑某些功能。示例:存在一个“文章”实体,其中包含基础表中最重要字段的约20个属性。该实体还包含一个OwnedObject“ StorageDetails”,包装了更多字段(以及所有与存储内容有关的功能)。
问题:我找不到控制是否应立即加载拥有对象的方法。对于其中的一些,我希望使用Include()显式加载它们。
public class Article : EntityBase
{
public string ArticleNumber { get;set; }
// Owned object, shares article number as key.
public StorageDetails StorageStuff { get; set; }
// An Entity from another table having a foreign key reference
public SomeOtherEntity OtherStuff { get; set; }
}
public class StorageDetails : OwnedObject<Article>
{
public Article Owner { get; set; }
}
// Somewhere during model creation ...
builder.OwnsOne(article => article.StorageStuff);
builder.HasOne(article => article.OtherStuff )
...
Run Code Online (Sandbox Code Playgroud)
使用OwnsOne定义模型并立即加载文章会加载StorageStuff。要加载OtherThing,我必须在查询中将它添加到Inlcude()中,这基本上是我要为拥有的对象实现的功能。 …