在我的EF实现Generic存储库中找不到.Include()方法

Nir*_*man 8 entity-framework repository-pattern

我使用Generic存储库从上层包装DbContext和DbSet类.但是,在某些查询中,我需要使用".Include()"方法来包含导航属性.但我无法在存储库方法上找到这些方法来恢复IQueryable

喜欢,

this.repository.GetQuery<GeneralCalendarDates>()
Run Code Online (Sandbox Code Playgroud)

这没有include方法,虽然我可以在这里使用.ToList().

知道这里有什么不对吗?

Sla*_*uma 24

Includefor IQueryable<T>是在程序集中的命名空间System.Data.Entity中实现的扩展方法EntityFramework.dll.所以你的项目必须引用这个程序集,你必须添加

using System.Data.Entity;
Run Code Online (Sandbox Code Playgroud)

在代码文件的开头.它将使基于字符串和lambda的版本Include可用,以便您可以使用:

orderQuery.Include("Customer")
Run Code Online (Sandbox Code Playgroud)

要么

orderQuery.Include(o => o.Customer)
Run Code Online (Sandbox Code Playgroud)

  • 或使用 Microsoft.EntityFrameworkCore;如果 .NET 核心 (2认同)