我正在做一个Asp.net core 2.2
项目,在ViewModel中加载相关数据时遇到了问题。
首先,我有一个名为QuestionTbl
的表:
public class Question
{
[Key]
public int questionID { get; set; }
public string questionTitle { get; set; }
public string GroupID { get; set; }
}
如您所见,我在Question
表中有一个名为GroupID
的string
属性,它显示每个问题的分组。
例如的
1 row in questionTbl
---------
questionID = 1
questionTitle = 'What is Your Name ?'
GroupID = '3,4,5'
在上面的例子中,ID = 1
的问题在3组(3 and 4 and 5)
中。
和GroupTbl
:
public class Group
{
[Key]
public int GroupID { get; set; }
public string GroupName { get; set; }
}
现在,我想要显示相关组的问题列表。
我有一个这样的ViewModel
:
public class GetQuestionViewModel
{
public int questionID { get; set; }
public string questionTitle { get; set; }
public ICollection<Group> QuestionGroups { get; set; }
}
和我的实体框架查询:
var questionQuery = (from a in _db.QuestionTbl
select new GetQuestionViewModel()
{
questionID = a.questionID,
questionTitle = a.questionTitle
})
.Include(q => q.QuestionGroups)
.ToList();
我想要一个包含问题和每个问题组的列表。但是在我的查询中,QuestionGroups
返回null。我也阅读了this链接,但它对我没有帮助。
发布于 2019-06-20 13:46:03
在这里,您不能直接将Include
用于viewModel。我建议您可以将many-to-many relationship用于您的问题和组模型。
型号:
public class Question
{
[Key]
public int questionID { get; set; }
public string questionTitle { get; set; }
public List<QuestionGroup> QuestionGroups { get; set; }
//public List<Group> Groups { get; set; }
//public string GroupID { get; set; }
}
public class Group
{
[Key]
public int GroupID { get; set; }
public string GroupName { get; set; }
public List<QuestionGroup> QuestionGroups { get; set; }
}
public class QuestionGroup
{
public int QuestionId { get; set; }
public Question Question { get; set; }
public int GroupId { get; set; }
public Group Group { get; set; }
}
public class GetQuestionViewModel
{
public int questionID { get; set; }
public string questionTitle { get; set; }
public ICollection<Group> QuestionGroups { get; set; }
}
DbContext;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<QuestionGroup>()
.HasKey(t => new { t.QuestionId, t.GroupId });
modelBuilder.Entity<QuestionGroup>()
.HasOne(qg => qg.Question)
.WithMany(q => q.QuestionGroups)
.HasForeignKey(qg => qg.QuestionId);
modelBuilder.Entity<QuestionGroup>()
.HasOne(qg=>qg.Group)
.WithMany(g => g.QuestionGroups)
.HasForeignKey(qg => qg.GroupId);
}
EF核心查询;
var questionQuery = (from a in _context.QuestionTbl.Include(q => q.QuestionGroups)
select new GetQuestionViewModel()
{
questionID = a.questionID,
questionTitle = a.questionTitle,
QuestionGroups = a.QuestionGroups.Select(qg => qg.Group).ToList()
})
.ToList();
发布于 2019-06-20 03:40:00
按照Microsoft Docs规格化表
问题
public class Question
{
[Key]
public int questionID { get; set; }
public string questionTitle { get; set; }
public int GroupID { get; set; }
[ForeignKey("Group_ID")]
public virtual Group Group { get; set; }
}
组
public class Group
{
[Key]
public int GroupID { get; set; }
public string GroupName { get; set; }
public virtual List<Question> questions { get; set; }
}
此时,您可以像这样查询记录:
db.Question.Include(q => q.Group)
https://stackoverflow.com/questions/56674220
复制相似问题