- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在尝试使用 Entity Framework,在遇到以下错误后,我尝试使用 ThenInclude 来解决它。
The expression '[x].ModelA.ModelB' passed to the Include operator could not be bound
但现在我似乎对它解决问题的原因缺乏一些理解
这有什么区别:
.Include(x => x.ModelA.ModelB)
还有这个:
.Include(x => x.ModelA).ThenInclude(x => x.ModelB)
最佳答案
“Include”适用于对象列表,但如果您需要获取多级数据,那么“ThenInclude”是最合适的。让我用一个例子来解释它。假设我们有两个实体,公司和客户:
public class Company
{
public string Name { get; set; }
public string Location { get; set; }
public List<Client> Clients {get;set;}
}
public class Client
{
public string Name { get; set; }
public string Domains { get; set; }
public List<string> CountriesOfOperation { get; set; }
}
现在如果您只需要公司和该公司的整个客户列表,您可以只使用“包括”:
using (var context = new YourContext())
{
var customers = context.Companies
.Include(c => c.Clients)
.ToList();
}
但是如果你想要一个以“CountriesOfOperation”作为相关数据的公司,你可以在包括客户之后使用“ThenInclude”,如下所示:
using (var context = new MyContext())
{
var customers = context.Companies
.Include(i => i.Clients)
.ThenInclude(a => a.CountriesOfOperation)
.ToList();
}
关于c# - 使用包含与 ThenInclude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49668851/
众所周知,EF-Core 中没有延迟加载。因此,这意味着我不得不事后才进行查询。既然要思考,那我就试着好好的去做吧。 我有一个相当标准的更新查询,但我想嘿,我不必总是包含 HeaderImage 和
我一直在尝试使用 Entity Framework,在遇到以下错误后,我尝试使用 ThenInclude 来解决它。 The expression '[x].ModelA.ModelB' passed
这是我的通用存储库: public class Repository : IRepository where T : BaseEntity { private DbContext _dbCon
我将.NET Framework(EF6)代码传输到ASP.NET Core(EF Core),但我偶然发现了这个问题。这是一些示例代码: 在EF6中,我使用Include()和Select()进行预
有没有人看到我做错了什么? ProjectActivityTasks 具有 UnitOfMeasureId 和 ProjectActivityTaskTypeId。按照它的编写方式,它认为 UnitO
我看到一些包含 ThenInclude 的 LINQ 代码示例。我在哪里可以找到这样的方法?在外部库或其他命名空间中。 我想用这样的方法获取派生数据,但是我找不到这样的方法或者你知道其他解决方案吗
这个问题在这里已经有了答案: Filtering on Include in EF Core (9 个回答) 关闭 5 个月前。 我有 3 个实体: Questionnaire.cs: public
我使用以下查询根据需要过滤我的 BranchId。 var query = await _dbContext.TargetItems .Include(i => i.Br
我有一个这样的查询 return await _ctx.Activities .Include(a => a.Attributes) .Include(
我的 IQueryable 看起来像这样: IQueryable query = context.Set(); query = query.Include("Car").ThenInclude("M
英孚核心 3.1 我见过Specification例如,并希望实现 ThenInclude 模式。 public static class QuerySpecificationExtensions {
我正在创建一个 ASP.NET Core API 应用程序,并依赖于 EF Core。我有这样定义的实体: public class AppUser : IdentityUser { publ
我将 Entity Framework Core 与 Repository Pattern 一起使用,但遇到一个问题。 我有类 Customer、Company 和 Email,其中隐藏了与此处无关的
假设首先有这些模型: Method 有一个 OriginalCodeOriginalCode 有很多 MutantMutant 有许多 ParseSubTree 现在,当查询 Method 时,我希望
这个问题在这里已经有了答案: Can not load related data with Include or ThenInclude or Select/Many with ONE query
我需要在 ThenInclude 中使用 where var templatesFields = await _context.Sections .Include(x
假设首先拥有这些模型: Method有一个 OriginalCodeOriginalCode有很多Mutant秒Mutant有很多ParseSubTree秒 现在在 Method 上查询时我想要另一个
以前(使用 .net 4.5.2 和 EF 6 时)。我有一个通用的 Get接受多个包含的方法如下; public abstract class DataContext : IdentityDbCon
好的,所以我有一个表 Building,其中包含该建筑物中的所有人员。然而,每个人都有一个职业,它本身就是一个实体,根据可用的内容添加到 Person 实体。 var data = _dbcontex
我正在使用 EF core 2.0 并想过滤子集合。谁能帮助我如何在 EF 核心 2.0 中执行此操作? var items = await _context.RiskType
我是一名优秀的程序员,十分优秀!