我的 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;
}
现在,我有一个存储库类,它有一个 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");
我希望为每个群组返回问题组和孩子问题。 问题是,它不会这样做,它会获得问题组和群组问题等等。
像这样:问题组&gt;问题&gt; [0]&gt;问题组&gt;问题
有谁知道为什么以及如何阻止它?
好的,现在我已启用延迟加载,我仍然遇到同样的问题:
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 有一个导航属性:问题
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; }
}
答案有一个导航属性:状态,条件也有一个导航属性,也恰好是状态< /强>:
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; }
}
州有一个导航属性,即过滤器:
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; }
}
和过滤器没有导航属性。 当我尝试使用延迟加载时,我希望只会填充我的导航属性,但如果我尝试获取 QuestionGroup ,则会获得问题所有答案和标准,这很好,但它也是 QuestionGroup 太
答案 0 :(得分:1)
您可以通过替换
来打破单向导航public QuestionGroup QuestionGroup {get;组; }
带
public int QuestionGroupId {get;组; }
这允许您查询以获取问题的问题组,但不会导致循环引用。您甚至可以将查询封装在方法中。
公共QuestionGroup GetQuestionGroup();