【问题标题】:Entity framework Code First multiple mappings of one entity type inside of another entity type实体框架 Code First 一种实体类型在另一种实体类型内的多重映射
【发布时间】:2013-11-16 04:23:57
【问题描述】:

我正在处理实体框架代码优先项目,但在创建映射时遇到了一些问题。一点背景:

该数据库将用于在线调查演示。对于这个问题,知道我们有 Question 和 QuestionAnswerOption 实体很有用。在问题实体上,我们首先需要知道当前问题的可用 QuestionAnswerOptions。其次,我们想知道是否有先决条件问题,然后如果有我们需要知道允许显示或隐藏此问题的 pre-req 问题中可接受的 QuestionAnswerOptions 是什么。

希望这是有道理的,但我会放一些模拟代码,这样你就可以看到我们到目前为止所拥有的大部分内容。

public class Question
{
    public virtual List<QuestionAnswerOption> AvailableQuestionAnswerOptions { get; set; }
    public Question RequiredPrerequisiteQuestion { get; set; }
    public virtual List<QuestionAnswerOption> AcceptablePrerequisiteQuestionAnswerOptions { get; set; }
}

public class QuestionAnswerOption
{
    public int Index { get;set; }
    public string Value { get;set; } // this may be 'Yes', 'No', 'Maybe', 'Don't Know', etc.
    public virtual List<Question> Questions { get; set; }
}

// then inside of our database context class we have the following
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<Question>().HasMany(x =>
        x.AvailableQuestionAnswerOptions).WithMany(y => y.Questions)
        .Map(m =>
        {
            m.MapLeftKey("Question_ID");
            m.MapRightKey("QuestionAnswerOption_ID");
            m.ToTable("AvailableQuestionAnswerOptionMap");
        });

    modelBuilder.Entity<Question>().HasMany(x =>
        x.AcceptablePrerequisiteQuestionAnswerOptions).WithMany(y => y.Questions)
        .Map(m =>
        {
            m.MapLeftKey("Question_ID");
            m.MapRightKey("QuestionAnswerOption_ID");
            m.ToTable("AcceptableQuestionAnswerOptionMap");
        });
}

在执行此操作后尝试创建迁移时,我们收到以下错误:

指定的架构无效。错误:未加载关系“XYZ.Database.Question_AvailableQuestionAnswerOptions”,因为“XYZ.Database.QuestionAnswerOption”类型不可用。

但是,如果我们注释掉 2 个 modelBuilder.Entity 调用中的任何一个,迁移就会顺利进行。有没有办法让这两者同时工作?或者更重要的是,有没有更好的方法来做我们正在尝试使用 EF Code First 做的事情?

谢谢

【问题讨论】:

    标签: entity-framework code-first entity-framework-migrations fluent-interface


    【解决方案1】:

    我在上面睡觉后能够弄清楚这一点。因此,对于遇到类似情况的其他人来说,这里是新代码。

    // No change to this class
    pubic class Question
    {
        public virtual List<QuestionAnswerOption> AvailableQuestionAnswerOptions { get; set; }
        public Question RequiredPrerequisiteQuestion { get; set; }
        public virtual List<QuestionAnswerOption> AcceptablePrerequisiteQuestionAnswerOptions { get; set; }
    }
    
    // Added second collection of Question, PreRequisiteQuestions
    public class QuestionAnswerOption
    {
        public int Index { get;set; }
        public string Value { get;set; } // this may be 'Yes', 'No', 'Maybe', 'Don't Know', etc.
        public virtual List<Question> Questions { get; set; }
        public virtual List<Question> PreRequisiteQuestions { get; set; }
    }
    
    // then inside of our database context class we have the following
    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
        modelBuilder.Entity<Question>().HasMany(x =>
            x.AvailableQuestionAnswerOptions).WithMany(y => y.Questions)
            .Map(m =>
            {
                m.MapLeftKey("Question_ID");
                m.MapRightKey("QuestionAnswerOption_ID");
                m.ToTable("AvailableQuestionAnswerOptionMap");
            });
    
        // changed this to use the new PreRequisiteQuestions collection
        modelBuilder.Entity<Question>().HasMany(x =>
            x.AcceptablePrerequisiteQuestionAnswerOptions).WithMany(y => y.PreRequisiteQuestions )
            .Map(m =>
            {
                m.MapLeftKey("Question_ID");
                m.MapRightKey("QuestionAnswerOption_ID");
                m.ToTable("AcceptableQuestionAnswerOptionMap");
            });
    }
    

    这让我得到了我正在寻找的结果,但我仍然想知道是否有更好的方法来做到这一点?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2013-04-04
      • 2023-03-29
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      相关资源
      最近更新 更多