简体   繁体   English

c# Entity Framework Eager Loading 拉入所有孩子

[英]c# Entity Framework Eager Loading pulling in all children

I have this questionGroup that looks something like this:我有这个questionGroup看起来是这样的:

public class QuestionGroup
{
    public int Id { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public QuestionGroupType Type { get; set; }

    public IList<Question> Questions { get; set; }
}

and the question model looks something like this:问题模型看起来像这样:

public class Question
{
    public int Id { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public int QuestionGroupId { get; set; }

    public QuestionGroup QuestionGroup { get; set; }
}

I have disabled Lazy Loading in my DbContext like this:我在DbContext 中禁用了延迟加载,如下所示:

/// <summary>
/// Default constructor
/// </summary>
public DatabaseContext()
    : base("DefaultConnection")
{

    // Disable Lazy Loading
    this.Configuration.LazyLoadingEnabled = false;

    // Write our SQL to the debug window
    this.Database.Log = s => Debug.WriteLine(s);

    // Increase the timeout
    this.Database.CommandTimeout = 10000;
}

Now, I have a Repository class that has a List method which looks like this:现在,我有一个Repository类,它有一个List方法,如下所示:

/// <summary>
/// Gets all the entities
/// </summary>
/// <param name="includes">Option includes for eager loading</param>
/// <returns></returns>
public IQueryable<T> List(params string[] includes)
{

    // Create a query
    IQueryable<T> query = this.dbEntitySet;

    // For each include, append to our query
    foreach (var include in includes)
        query = query.Include(include);

    // Return our query
    return query;
}

So that in theory I can send an array of includes to the Repository so that they can be Eagerly loaded .因此,在理论上我可以给数组包含,让他们可以即时加载 Somewhere done the line I do this:在某处完成了我这样做的行:

var models = await this._service.ListAllAsync("Questions");

Which I would expect to return my QuestionGroups and the child Questions for each group.我希望为每个组返回我的问题组和子问题 The problem is, it doesn't just do that, it will Get the Question's group and the groups Question and so on and so on.问题是,它不只是那样做,它会获取问题的组和组问题等等。

like this:像这样:

QuestionGroup > Questions > [0] > QuestionGroups > Questions问题组 > 问题 > [0] > 问题组 > 问题

Does anyone know why and how to stop it?有谁知道为什么以及如何阻止它?


Update更新

Ok, so now I have enabled Lazy loading and I still get the same issue:好的,现在我启用了延迟加载,但仍然遇到相同的问题:

public class QuestionGroup
{
    public int Id { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public QuestionGroupType Type { get; set; }

    public virtual IList<Question> Questions { get; set; }
}

A QuestionGroup has one navigational property: Questions QuestionGroup有一个导航属性: Questions

public class Question
{
    public int Id { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public int QuestionGroupId { get; set; }

    public QuestionGroup QuestionGroup { get; set; }
    public virtual IList<Answer> Answers { get; set; }
    public virtual Criteria Criteria { get; set; }
}

A Question has 2 navigational properties: Answers and Criteria :一个问题有 2 个导航属性:答案标准

public class Answer
{
    public int Id { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public int QuestionId { get; set; }
    public int Order { get; set; }

    public Question Question { get; set; }
    public virtual IList<State> States { get; set; }
}

public class Criteria
{
    public int Id { get; set; }
    [Required] public CriteriaType Type { get; set; }
    [Required] [MaxLength(100)] public string Name { get; set; }
    public bool SaveToDatabase { get; set; }

    public virtual IList<State> States { get; set; }
    public IList<Question> Questions { get; set; }
}

An Answer has one navigational property: States , and Criteria also has one navigational property which also happens to be States :答案有一个导航属性: States ,而Criteria也有一个导航属性,它也恰好是States

public class State
{
    public int Id { get; set; }
    public int CriteriaId { get; set; }
    [Required] [MaxLength(100)] public string Name { get; set; }
    public TargetType Target { get; set; }

    public virtual IList<Filter> Filters { get; set; }
    public Criteria Criteria { get; set; }
}

A State has one navigational property, which is Filter :一个State有一个导航属性,即Filter

public class Filter
{
    public int Id { get; set; }
    [Required] [MaxLength(5)] public string Operator { get; set; }
    [Required] [MaxLength(255)] public string Expression { get; set; }
    [MaxLength(100)] public string Field { get; set; }
    public int StateId { get; set; }

    public State State { get; set; }
}

and Filters have no navigational properties.过滤器没有导航属性。 When I try to use Lazy Loading , I expect that only my navigation properties will be populated, but if I try to get the QuestionGroup , it will get the Question with all it's Answers and Criteria , which is fine, but it also get's the QuestionGroup too.当我尝试使用Lazy Loading 时,我希望只会填充我的导航属性,但是如果我尝试获取QuestionGroup ,它将获取包含所有AnswersCriteriaQuestion ,这很好,但它也会获取QuestionGroup也。

You break the uni-directional navigation by replacing您通过替换来打破单向导航

public QuestionGroup QuestionGroup { get;公共问题组问题组{得到; set;放; } }

with

public int QuestionGroupId { get;公共 int QuestionGroupId { 获取; set;放; } }

This allows you to query back to get the question group of the question, but not cause a circular reference.这允许您回查询以获取问题的问题组,但不会导致循环引用。 You can even encapsulate the query in a method.您甚至可以将查询封装在一个方法中。

public QuestionGroup GetQuestionGroup();公共问题组 GetQuestionGroup();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM