我正在研究一个实体框架代码第一项目,并创建映射的一些问题。一点背景:

数据库将用于在线调查演示。出于这个问题的目的,知道我们有问题和质量答题有用。关于问题实体,我们需要首先知道资金问题的可用质量问题。其次,我们想知道是否存在预先要求的问题,然后我们需要知道从预追溯问题中获取的可接受的Questansweroptions是什么,允许显示或隐藏此问题。

希望这是有道理的,但我会把一些模拟代码放了,所以你可以看到我们到目前为止的东西。

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代码吗?

感谢

有帮助吗?

解决方案

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top