Asp.net核心如何在ViewModel Entity Framework中加载相关数据

时间:2019-06-19 18:58:30

标签: c# entity-framework asp.net-core entity-framework-core

我正在从事一个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; }
}

如您所见,我在string表中有一个名为GroupID的{​​{1}}属性,该属性显示每个问题的组。

例如

Question

在上面的示例中,带有1 row in questionTbl --------- questionID = 1 questionTitle = 'What is Your Name ?' GroupID = '3,4,5' 的问题分为3个组ID = 1

还有(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(); 在我的查询中返回null。我也阅读了this链接,但对我没有帮助。

2 个答案:

答案 0 :(得分:0)

按照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)

答案 1 :(得分:0)

您不能在此处将Include直接用于viewModel。我建议您可以将many-to-many relationship用于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();