c#实体框架将所有子项拉入
我有一个问题组,看起来像这样:c#实体框架将所有子项拉入,c#,entity-framework,C#,Entity Framework,我有一个问题组,看起来像这样: 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 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; }
}
/// <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;
}
/// <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;
}
我在我的DbContext中禁用了延迟加载,如下所示:
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; }
}
/// <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;
}
/// <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;
}
我希望返回我的问题组和每个组的孩子的问题。
问题是,它不仅会这样做,还会得到问题的组和组问题等等
像这样:
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; }
}
/// <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;
}
/// <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;
}
问题组>问题>[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; }
}
和过滤器没有导航属性。
当我尝试使用延迟加载时,我希望只填充我的导航属性,但是如果我尝试获取问题组,它将获取问题,以及所有答案和标准,这很好,但是它也得到了问题组。你通过替换 公共问题组问题组{get;set;} 与 public int QuestionGroupId{get;set;} 这允许您查询回该问题的问题组,但不会导致循环引用。您甚至可以将查询封装在方法中 公共问题组GetQuestionGroup()
这就是急切加载的工作原理。你要么得到全部,要么一无所获。这有什么问题?我得到了一个stackoverflow我想你已经想到了某种序列化(json?),因为这样在查询本身中不会发生?如果是,您可以通过使用序列化程序选项(如Newtonsoft Json.JsonSerializer
preserverencesshandling
)来解决此问题