[英]Loading related data in Entity Framework Core and ASP.NET Core
[英]Asp.net core how to loading related data in viewmodel Entity Framework
我正在研究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
屬性,該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。 我也閱讀了此鏈接,但對我沒有幫助。
按照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)
您不能將Include
here直接用於viewModel。我建議您可以對Question和Group模型使用多對多關系 。
楷模:
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 Core查詢;
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.