엔티티 프레임 워크 코드 다른 엔티티 유형의 내부의 하나의 엔터티 유형의 첫 번째 여러 매핑

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

문제

엔티티 프레임 워크 코드 첫 번째 프로젝트에서 일하고 맵핑을 만드는 몇 가지 문제가 발생합니다. 작은 배경 :

데이터베이스는 온라인 조사 데모에 사용됩니다. 이 질문의 목적을 위해 우리는 질문과 quystansweroption 엔티티가 있음을 알면 유용합니다. 질문 엔티티에서 우리는 먼저 resilrent 질문에 사용 가능한 quystalsweroptions를 알아야합니다. 둘째, 우리는 사전 요구 사항이 있는지 여부를 알고 싶습니다. 그런 다음이 질문을 표시하거나 숨길 수있는 사전 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.questionansweroption'유형이 사용할 수 없기 때문에 'xyz.database.question_availableQuestionAnsionAnswerOptions'관계가로드되지 않았습니다.

그러나 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