c# - EntityFramework Core 1.1.0 缺少 Include()?

标签 c# entity-framework entity-framework-core

<分区>

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

https://learn.microsoft.com/en-us/ef/core/querying/related-data

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() 选项。

知道为什么缺少这个或如何加载外键关系数据吗?

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

我已经求助于显式加载关系数据。这对于小结果集来说很好,但随着我的数据集的增长,这会让我很伤心。

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

最佳答案

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

From the repository linked in the documentation :

using Microsoft.EntityFrameworkCore;
using System.Linq;

关于c# - EntityFramework Core 1.1.0 缺少 Include()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42098706/

相关文章:

c# - 如何在 Entity Framework 4 中切换 SQL 和 SQL CE?或者如何以编程方式更改 edmx 文件中的 "Schema Namespace"

.net - EF Core linq 和条件包含然后包含问题

entity-framework-core - EF Core 3.1 使用 Authentication=Active Directory 集成

c# - 将密码添加到 Xamarin 表单中的 Sqlite 文件

c# - 每当通过 Web 应用程序将记录插入 SQL Server 时,触发 winforms 程序运行

c# - 如何将 UI RawImage 转换为字节数组

c# - 如何在没有源代码的情况下将 .ipa 或 .app 的体系结构从 arm64 更改为 i386

c# - 事务和锁

c# - 实体的主键字段设置为 0 而不是 null

c# - EF Core 2.2 新迁移不断删除和重新创建种子数据