简体   繁体   English

实体框架:获取子代

[英]Entity Framework: Get children

I have the following Entity classes: 我有以下实体类:

public class History : BaseEntity
    {
        public string Version { get; set; }
        public DateTime? ReleaseDate { get; set; }
        public string Name { get; set; }
        public string Additional { get; set; }
        public List<HistoryEntry> Entries { get; set; }
    }

    public class HistoryEntry : BaseEntity
    {
        public string Version { get; set; }        
        public string BugNr { get; set; }
        public AllowedTypes EntryType { get; set; }
        public string Description { get; set; }
        public History Parent { get; set; }
    }

    public enum AllowedTypes
    {
        Bug, Enhancement
    }

which are mapped that way: 映射方式是:

 public HistoryMap()
        {
            ToTable("History");
            HasKey(c => c.Version);
            Property(c => c.Version).HasMaxLength(10);
            Property(c => c.Name).HasMaxLength(200);
        }

this results in two tables that act exactly like I wanted ("History" has "Version" as primary key and "HistoryEntry" has a foreign Key "Version" that is linked to "History"."Version" 这将导致两个表的运行方式完全符合我的需要(“历史记录”具有“版本”作为主键,“历史记录条目”具有与“历史记录”链接的外键“版本”。“版本”

After Adding some stuff into these tables I try to read the contents: 在向这些表中添加一些内容之后,我尝试读取其内容:

IQueryable<History> query = _historyRepository.Table.OrderByDescending(c => c.ReleaseDate);
 var historyList = new List<Core.Domain.Tengo.History>(query);

While all History-Entries are read successfully, the "Entries"-Property if the "History"-Object is always NULL. 成功读取所有历史记录条目后,如果“历史记录”对象始终为NULL,则“条目”属性。

How do I achieve that the linked Items are also read and stored into Entries? 如何实现链接的项目也被读取并存储到条目中?

Navigation properties such as ICollection<T> / IList<T> should be marked as virtual . 导航属性(例如ICollection<T> / IList<T>应标记为virtual This allows EntityFramework to override them and provide code for lazy-loading the properties. 这允许EntityFramework覆盖它们,并提供用于延迟加载属性的代码。

So the line (in the History class) 所以这行(在History类中)

public List<HistoryEntry> Entries { get; set; }

Should become 应该成为

public virtual List<HistoryEntry> Entries { get; set; }

Yves answer is correct if you want to use Lazy Loading. 如果您要使用延迟加载,Yves答案是正确的。

The other way to do this is to use Eager Loading. 另一种方法是使用“急切加载”。 Eager loading loads the properties immediately rather than when it is accessed. 急切的加载会立即加载属性,而不是在访问属性时加载。 The snippet below uses Eager Loading. 下面的代码段使用“急切加载”。

_historyRepository.Table.Include(x=>x.Entries).OrderByDescending(c => c.ReleaseDate);

The difference between Lazy and eager loading are explained in the link below: 延迟加载和渴望加载之间的区别在下面的链接中进行了说明:

http://www.dotnet-tricks.com/Tutorial/entityframework/RIWW210913-Difference-between-Lazy-Loading-and-Eager-Loading.html http://www.dotnet-tricks.com/Tutorial/entityframework/RIWW210913-Difference-between-Lazy-Loading-and-Eager-Loading.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM