Child Table 属性未在 ef 核心继承中获取
Child Table Properties are not fetching in ef core inheritance
我有以下实体..
public class Paper
{
public int Id { get; set; }
public string PaperCode { get; set; }
...
}
public class MCQPaper : Paper
{
public ICollection<MCQQuestion> Questions { get; set; }
}
public class MCQQuestion : Question
{
public int MCQPaperId { get; set; }
public MCQPaper MCQPaper { get; set; }
public int? MCQOptionId { get; set; }
[ForeignKey("MCQOptionId")]
public MCQOption TrueAnswer { get; set; }
public ICollection<MCQOption> MCQOptions { get; set; }
}
public class MCQOption
{
public int Id { get; set; }
public string OptionText { get; set; }
}
我正在尝试根据唯一的纸质代码获取 MCQPaper,但它总是给我空的问题集合
这是我在存储库中的查询..
public MCQPaper GetByPaperCode(string paperCode)
{
var ans = AppDbContext.MCQPapers
.Where(paper => paper.PaperCode.Equals(paperCode))
//.Include(paper => paper.Questions)
// .ThenInclude(que => que.MCQOptions)
.Include(paper => paper.Questions)
.ThenInclude(que => que.MCQPaper)
//.Include(paper => paper.Questions)
// .ThenInclude(que => que.TrueAnswer)
.FirstOrDefault();
return ans;
}
在这里我尝试了 include() 和 theninclude() 的各种组合,但是 none 它们对我有用
最后忽略语法错误
提前谢谢你
在发表评论并在 google 上搜索后,我找到了解决方案
使用两个查询可以解决这个问题,因为这里我在 MCQQuestion
和 MCQOption
之间有循环依赖
所以解决方案是....
public MCQPaper GetByPaperCode(string paperCode)
{
using var transaction = AppDbContext.Database.BeginTransaction();
MCQPaper ans = new MCQPaper();
try
{
ans = AppDbContext.MCQPapers
.FirstOrDefault(paper => paper.PaperCode.Equals(paperCode));
var questions = AppDbContext.MCQQuestions
.Include(que => que.MCQOptions)
.Where(que => que.MCQPaperId == ans.Id);
ans.Questions = questions.ToList();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
}
return ans;
}
我有以下实体..
public class Paper
{
public int Id { get; set; }
public string PaperCode { get; set; }
...
}
public class MCQPaper : Paper
{
public ICollection<MCQQuestion> Questions { get; set; }
}
public class MCQQuestion : Question
{
public int MCQPaperId { get; set; }
public MCQPaper MCQPaper { get; set; }
public int? MCQOptionId { get; set; }
[ForeignKey("MCQOptionId")]
public MCQOption TrueAnswer { get; set; }
public ICollection<MCQOption> MCQOptions { get; set; }
}
public class MCQOption
{
public int Id { get; set; }
public string OptionText { get; set; }
}
我正在尝试根据唯一的纸质代码获取 MCQPaper,但它总是给我空的问题集合
这是我在存储库中的查询..
public MCQPaper GetByPaperCode(string paperCode)
{
var ans = AppDbContext.MCQPapers
.Where(paper => paper.PaperCode.Equals(paperCode))
//.Include(paper => paper.Questions)
// .ThenInclude(que => que.MCQOptions)
.Include(paper => paper.Questions)
.ThenInclude(que => que.MCQPaper)
//.Include(paper => paper.Questions)
// .ThenInclude(que => que.TrueAnswer)
.FirstOrDefault();
return ans;
}
在这里我尝试了 include() 和 theninclude() 的各种组合,但是 none 它们对我有用
最后忽略语法错误
提前谢谢你
在发表评论并在 google 上搜索后,我找到了解决方案
使用两个查询可以解决这个问题,因为这里我在 MCQQuestion
和 MCQOption
所以解决方案是....
public MCQPaper GetByPaperCode(string paperCode)
{
using var transaction = AppDbContext.Database.BeginTransaction();
MCQPaper ans = new MCQPaper();
try
{
ans = AppDbContext.MCQPapers
.FirstOrDefault(paper => paper.PaperCode.Equals(paperCode));
var questions = AppDbContext.MCQQuestions
.Include(que => que.MCQOptions)
.Where(que => que.MCQPaperId == ans.Id);
ans.Questions = questions.ToList();
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
}
return ans;
}