Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架可选关系导致;冲突多重性“;_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 实体框架可选关系导致;冲突多重性“;

C# 实体框架可选关系导致;冲突多重性“;,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我首先将实体框架代码用于包含多项选择题的简单数据库。每个问题都有多个可能的答案,并指定一个为正确答案 public class Question { public int QuestionId { get; set; } [ForeignKey("CorrectAnswer")] public int CorrectAnswerId { get; set; } public string Text { get; set; } public virtua

我首先将实体框架代码用于包含多项选择题的简单数据库。每个问题都有多个可能的答案,并指定一个为正确答案

public class Question
{
    public int QuestionId { get; set; }

    [ForeignKey("CorrectAnswer")]
    public int CorrectAnswerId { get; set; }
    public string Text { get; set; }

    public virtual ICollection<Answer> Answers { get; set; }

    public virtual Answer CorrectAnswer { get; set; }
}

public class Answer
{
    public int AnswerId { get; set; }
    [ForeignKey("Question")]
    public int QuestionId { get; set; }
    public string Text { get; set; }

    public virtual Question Question { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Answer>()
        .HasRequired(a => a.Question)
        .WithMany(q => q.Answers)
        .HasForeignKey(a => a.QuestionId)
        .WillCascadeOnDelete(false);
}
公开课问题
{
public int QuestionId{get;set;}
[外键(“正确答案”)]
public int CorrectAnswerId{get;set;}
公共字符串文本{get;set;}
公共虚拟ICollection答案{get;set;}
公共虚拟应答更正应答{get;set;}
}
公开课答案
{
public int AnswerId{get;set;}
[外键(“问题”)]
public int QuestionId{get;set;}
公共字符串文本{get;set;}
公共虚拟问题{get;set;}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.HasRequired(a=>a.Question)
.有很多(q=>q.答案)
.HasForeignKey(a=>a.QuestionId)
.WillCascadeOnDelete(假);
}
这一切都很好,但我需要避免第二十二条军规的情况,即你不能创建一个问题,因为它需要一个正确的答案,但你不能创建一个答案,因为它需要一个问题。所以我想让CorrectAnswerId字段为空,允许您创建问题,然后是答案,然后指定正确答案

public class Question
{
    public int QuestionId { get; set; }

    [ForeignKey("CorrectAnswer")]
    public int CorrectAnswerId { get; set; }
    public string Text { get; set; }

    public virtual ICollection<Answer> Answers { get; set; }

    public virtual Answer CorrectAnswer { get; set; }
}

public class Answer
{
    public int AnswerId { get; set; }
    [ForeignKey("Question")]
    public int QuestionId { get; set; }
    public string Text { get; set; }

    public virtual Question Question { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Answer>()
        .HasRequired(a => a.Question)
        .WithMany(q => q.Answers)
        .HasForeignKey(a => a.QuestionId)
        .WillCascadeOnDelete(false);
}
我尝试了以下方面的变化,但总是出现“冲突的多重性”异常:

modelBuilder.Entity<Question>()
            .HasOptional(q => q.CorrectAnswer)
            .WithRequired(a => a.Question)
            .Map(p => p.MapKey("CorrectAnswerId"));
modelBuilder.Entity()
.has可选(q=>q.CorrectAnswer)
.WithRequired(a=>a.问题)
.Map(p=>p.MapKey(“CorrectAnswerId”);
更改

 int CorrectAnswerId {get;set;}


谢谢,这个有用。我本应该亲自尝试,但我看到的另一个问题是,它不起作用。:)也许是另一种情况?不管怎么说,我正试图让同样的关系运作起来。我正在使用EF6,我知道我试图精确地构建您的代码,但它不起作用。可能EF6有一些变化?!