【发布时间】:2020-04-11 07:00:24
【问题描述】:
我使用 EF 和 EF Core 已经有很长时间了,但总有一些问题一直给我带来问题,无论是哪种方式,那就是在查询中使用方法,特别是在映射时。
例如,现在我有这个代码
public IEnumerable<SelectObjDTO> SelectSearch()
{
return Context.Usuarios.IncludePerson().ToSelectObjDTO();
}
在不同的班级
public static IQueryable<Users> IncludePerson(this IQueryable<Usuers> Query)
{
return Query.Include(x => x.Persons );
}
public static SelectObjDTO ToSelectObjDTO(this Persons POCO)
{
return new SelectObjDTO()
{
Id = POCO.Users.FirstOrDefault().Id,
FirstName= POCO.DocumentNumber+ " - " + POCO.FirstName + " " + POCO.LastName
};
}
public static IEnumerable<SelectObjDTO> ToSelectObjDTO(this IEnumerable<Persons> Query)
{
return Query.Select(POCO => POCO.ToSelectObjDTO());
}
函数IEnumerable<SelectObjDTO> ToSelectObjDTO(this IEnumerable<Persons> Query) 返回错误,因为Users 为空。
但在IEnumerable<SelectObjDTO> ToSelectObjDTO(this IEnumerable<Persons> Query) 中,查询确实在Users 列表中包含某些内容。
这是这种情况,但在我遇到其他问题之前,例如来自 EF Core 的错误,它基本上表示上下文在同一个查询中运行了两次(真的很抱歉,记不清了它是怎样的),这是通过包含正在调用的任何导航属性来修复的。
以防万一,我正在使用延迟加载代理。
谢谢
【问题讨论】:
-
在处理变量之前将其加载到内存中。
-
如果我将它加载到内存中,查询将加载完整的实体,而不是只加载我需要的内容
标签: c# asp.net .net-core entity-framework-core ef-core-2.2