首页
学习
活动
专区
工具
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的表:

代码语言:javascript
复制
public class Question
{
    [Key]
    public int questionID { get; set; }
    public string questionTitle { get; set; }
    public string GroupID { get; set; }
}

如您所见,我在Question表中有一个名为GroupIDstring属性,它显示每个问题的分组。

例如的

代码语言:javascript
复制
1 row in questionTbl
---------
questionID = 1
questionTitle = 'What is Your Name ?'
GroupID = '3,4,5'

在上面的例子中,ID = 1的问题在3组(3 and 4 and 5)中。

GroupTbl

代码语言:javascript
复制
public class Group
{
    [Key]
    public int GroupID { get; set; }
    public string GroupName { get; set; }
}

现在,我想要显示相关组的问题列表。

我有一个这样的ViewModel

代码语言:javascript
复制
public class GetQuestionViewModel
{
    public int questionID { get; set; }
    public string questionTitle { get; set; }
    public ICollection<Group> QuestionGroups { get; set; }
}

和我的实体框架查询:

代码语言:javascript
复制
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

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-20 13:46:03

在这里,您不能直接将Include用于viewModel。我建议您可以将many-to-many relationship用于您的问题和组模型。

型号:

代码语言:javascript
复制
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;

代码语言:javascript
复制
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核心查询;

代码语言:javascript
复制
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();
票数 0
EN

Stack Overflow用户

发布于 2019-06-20 03:40:00

按照Microsoft Docs规格化表

问题

代码语言:javascript
复制
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; }
}

代码语言:javascript
复制
public class Group
{
    [Key]
    public int GroupID { get; set; }
    public string GroupName { get; set; }

    public virtual List<Question> questions { get; set; }
}

此时,您可以像这样查询记录:

代码语言:javascript
复制
db.Question.Include(q => q.Group)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56674220

复制
相关文章

相似问题

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