本文介绍了如何使用EF Core加载导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF6中,我们有这样的选择:

In EF6 we had such option:

context.Set<TEntity>().Attach(entity);
context.Entry(entity).Collection("NavigationProperty").Load();

由于EF Core 100%严格键入,因此它们删除了收集功能。但是应该用什么代替呢?

Since EF Core "goes 100% strictly typed" they have removed Collection function. But what should be used instead?

添加:
我的意思是如何为ATTACHED实体加载include /导航集合属性?

Added:I mean how to load includes/"navigation collection properties" for ATTACHED entity?

推荐答案

显式加载已在Entity Framework Core v1.1中添加。
请参见

Explicit Loading was added in Entity Framework Core v1.1.See Microsoft Docs

从文档中

using (var context = new BloggingContext())
{
    var blog = context.Blogs
    .Single(b => b.BlogId == 1);

    context.Entry(blog)
        .Collection(b => b.Posts)
        .Load();

    context.Entry(blog)
        .Reference(b => b.Owner)
        .Load();
}

这篇关于如何使用EF Core加载导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:32