簡體   English   中英

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

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

我有這個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; }
}

問題模型看起來像這樣:

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; }
}

我在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;
}

現在,我有一個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;
}

因此,在理論上我可以給數組包含,讓他們可以即時加載 在某處完成了我這樣做的行:

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

我希望為每個組返回我的問題組和子問題 問題是,它不只是那樣做,它會獲取問題的組和組問題等等。

像這樣:

問題組 > 問題 > [0] > 問題組 > 問題

有誰知道為什么以及如何阻止它?


更新

好的,現在我啟用了延遲加載,但仍然遇到相同的問題:

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; }
}

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; }
}

一個問題有 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; }
}

答案有一個導航屬性: 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; }
}

一個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; }
}

過濾器沒有導航屬性。 當我嘗試使用Lazy Loading 時,我希望只會填充我的導航屬性,但是如果我嘗試獲取QuestionGroup ,它將獲取包含所有AnswersCriteriaQuestion ,這很好,但它也會獲取QuestionGroup也。

您通過替換來打破單向導航

公共問題組問題組{得到; 放; }

公共 int QuestionGroupId { 獲取; 放; }

這允許您回查詢以獲取問題的問題組,但不會導致循環引用。 您甚至可以將查詢封裝在一個方法中。

公共問題組 GetQuestionGroup();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM