实体框架代码第一个在另一个实体类型内的一个实体类型的多个映射

时间:2013-11-16 04:23:58

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

我正在开发一个Entity Framework Code First项目,并且在创建映射时遇到了一些问题。一点背景:

该数据库将用于在线调查演示。出于这个问题的目的,知道我们有Question和QuestionAnswerOption实体是有用的。在问题实体上,我们需要首先了解有关问题的可用QuestionAnswerOptions。其次,我们想知道是否存在先决条件问题,然后我们是否需要知道从请求前问题中可接受的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&lt;&gt;中的任何一个调用迁移顺利进行。有没有办法让这两个同时工作?或者更重要的是,有没有更好的方法来完成我们尝试使用EF Code First做的事情?

由于

1 个答案:

答案 0 :(得分:0)

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

// 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");
        });
}

这让我得到了我想要的结果,虽然我仍然有兴趣知道是否有更好的方法来做到这一点?