首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Asp.net内核如何在视图模型实体框架中加载相关数据

Asp.net内核如何在视图模型实体框架中加载相关数据
EN

Stack Overflow用户
提问于 2019-06-20 02:58:31
回答 2查看 1K关注 0票数 1

我正在做一个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表中有一个名为GroupIDstring属性,它显示每个问题的分组。

例如的

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链接,但它对我没有帮助。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56674220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档