EntityFramework Core 1.1.0是否缺少Include()? [英] EntityFramework Core 1.1.0 missing Include()?

查看:131
本文介绍了EntityFramework Core 1.1.0是否缺少Include()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EntityFramework Core 1.1.0。我可以查询表并加载实体,但是Microsoft的说明指出是否要加载关系数据,应使用 .Include()函数:

I am using EntityFramework Core 1.1.0. I can query a table and load entities, but the instructions from Microsoft indicates if I want to load relational data, I should use the .Include() function:

https://docs.microsoft。 com / zh-CN / ef / core / querying / related-data


您可以使用 Include 方法,用于指定要包含在查询结果中的相关数据。在下面的示例中,结果中返回的博客将使用相关帖子填充其帖子属性。

You can use the Include method to specify related data to be included in query results. In the following example, the blogs that are returned in the results will have their Posts property populated with the related posts.

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .ToList();
}


我没有 .Include( )选项。

有什么想法为什么会丢失或如何加载外键关系数据?

Any ideas why this is missing or how to load foreign-key relational data?

this.context.Mail
    .Include("Files") // This is missing

我求助于显式加载关系数据。这对于较小的结果集很好,但是随着我的数据集的增长,这将使我感到沮丧。

I have resorted to explicitly loading relational data. This is fine for small result sets, but as my data sets grow, this is going to cause me grief.

var mails = this.context.Mail.ToList();
mails.ForEach(mail =>
{
    this.context.Entry(mail)              
    .Collection(m => m.Files)
    .Load();
});


推荐答案

您是否包含了正确的命名空间?

Have you included the correct namespaces?

从文档中链接的存储库中

using Microsoft.EntityFrameworkCore;
using System.Linq;

这篇关于EntityFramework Core 1.1.0是否缺少Include()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆