【发布时间】:2018-06-26 11:36:04
【问题描述】:
我打算在不使用 C#/实体框架中的包含的情况下加载相关实体。 在我的示例中,我使用左连接来加载所有问题选项,这些选项仅在我在 LINQ 查询的 select 语句中明确表示时才显示。我的问题是如何确保它加载相关实体而不在 LINQ 查询中的 select 中定义相关实体。
数据模型
public class QuestionDataModel : BasicDataModel
{
public QuestionDataModel()
{
QuestionOptions = new HashSet<QuestionOptionDataModel>();
}
public Guid Id { get; set; }
public virtual ICollection<QuestionOptionDataModel> QuestionOptions { get; set; }
}
LINQ 查询
var q1 = (
from question in Context.Questions
join options in Context.QuestionOptions on question.Id equals options.QuestionId into qo
where question.ConsultationId == Guid.Parse("10324003-0012-4D99-95D8-7E7189CA3888")
select new
{
question
//,qo // it only loads questionOption if qo is here, I need to do without that, since it is collection property in QuestionDataModel class
}
).ToList();
【问题讨论】:
-
你想加载实体包含还是不包含?
-
我想加载没有包含的实体!
-
如果您使用的是EF Core 2.1,您可以启用延迟加载,当您显式调用它时将加载相关实体。参考这个docs.microsoft.com/en-us/ef/core/querying/…
标签: linq entity-framework-core