エンティティフレームワークコード別のエンティティタイプ内部の1つのエンティティタイプの最初の複数のマッピング

StackOverflow https://stackoverflow.com//questions/20014656

質問

私はエンティティフレームワークコード最初のプロジェクトに取り組んでおり、いくつかの問題を作成しているマッピングを作成しています。ちょっとした背景:

データベースはオンライン調査デモに使用されます。この質問の目的のために、質問とアンケート州オプションのエンティティがあることを知っておくと便利です。質問エンティティでは、最初に耐震に関する質問のための利用可能な質問勢力を知る必要があります。第二に、前提条件の質問があるかどうかを知りたいのですが、この質問を表示または非表示にする前に許容される質問勢力が何であるかを知る必要がある場合は、

これは理にかなっているが、私はこれまでに持っているものをかなり多く見ることができるように、私はいくつかのモックコードを置くでしょう。

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.questionanswerOption 'が利用できないため、関係' xyz.database.question_availableQuestionAnswerOptions 'がロードされませんでした。

しかし、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