Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
与Entity-Framework-4.1的关系和外键错误_Entity Framework_Entity Framework 4_Entity Framework 4.1 - Fatal编程技术网

与Entity-Framework-4.1的关系和外键错误

与Entity-Framework-4.1的关系和外键错误,entity-framework,entity-framework-4,entity-framework-4.1,Entity Framework,Entity Framework 4,Entity Framework 4.1,我遇到如下描述的问题: One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'CorpQuestionA_CorpQAnswer_Source' in relationship 'CorpQuestionA_C

我遇到如下描述的问题:

One or more validation errors were detected during model generation:

System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'CorpQuestionA_CorpQAnswer_Source' in relationship 'CorpQuestionA_CorpQAnswer'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
我的数据库作为屏幕截图pic:

实体定义如下:

public class CorpQuestionA 
{ 
    [Key] 
    public Guid cqua_QuestionId { get; set; } 
    public Guid cqua_CorpId { get; set; } 
    [MaxLength] 
    public string cqua_Question { get; set; } 
    public DateTime cqua_Date { get; set; } 
    public Boolean cqua_IsAnswer { get; set; } 

    [ForeignKey("cqua_CorpId")] 
    public virtual CorpRegInfo510112 CorpRegInfo510112 { get; set; } 
    public virtual CorpQAnswer CorpQAnswer { get; set; } 
} 


public class CorpQAnswer 
{ 
    [Key] 
    public Guid cqan_QuestionId { get; set; } 
    public string cqan_Answer { get; set; } 

    [ForeignKey("cqan_QuestionId")] 
    public virtual CorpQuestionA CorpQuestionA { get; set; } 
} 
然后是ProjectDataEntities文件:

public class ProjectDataEntities : DbContext 
{ 
    public DbSet<CorpQuestionA> Tbl_CorpQuestionAs { get; set; } 
    public DbSet<CorpQAnswer> Tbl_CorpQAnswers { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        base.OnModelCreating(modelBuilder); 
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 

        modelBuilder.Entity<CorpQuestionA>().ToTable("tbl_CorpQuestionA"); 
        modelBuilder.Entity<CorpQAnswer>().ToTable("tbl_CorpQAnswer"); 

        //Todo: Add custom mapping rules here... 
        modelBuilder.Entity<CorpQuestionA>().HasOptional(p => p.CorpQAnswer).WithOptionalPrincipal(x => x.CorpQuestionA);//.Map(p => p.MapKey("cqua_QuestionId")); 
    } 
}

等待帮助时,thx将按如下方式更改共享主键

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    base.OnModelCreating(modelBuilder); 
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 

    modelBuilder.Entity<CorpQuestionA>().ToTable("tbl_CorpQuestionA"); 
    modelBuilder.Entity<CorpQAnswer>().ToTable("tbl_CorpQAnswer"); 

    modelBuilder.Entity<CorpQuestionA>().HasOptional(p => p.CorpQAnswer)
       .WithRequired(x => x.CorpQuestionA);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{ 
基于模型创建(modelBuilder);
modelBuilder.Conventions.Remove();
modelBuilder.Entity().ToTable(“tbl_CorpQuestionA”);
modelBuilder.Entity().ToTable(“tbl_CorpQAnswer”);
modelBuilder.Entity().has可选(p=>p.CorpQAnswer)
.需要(x=>x.CorpQuestionA);
}

如下更改共享主键

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    base.OnModelCreating(modelBuilder); 
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 

    modelBuilder.Entity<CorpQuestionA>().ToTable("tbl_CorpQuestionA"); 
    modelBuilder.Entity<CorpQAnswer>().ToTable("tbl_CorpQAnswer"); 

    modelBuilder.Entity<CorpQuestionA>().HasOptional(p => p.CorpQAnswer)
       .WithRequired(x => x.CorpQuestionA);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{ 
基于模型创建(modelBuilder);
modelBuilder.Conventions.Remove();
modelBuilder.Entity().ToTable(“tbl_CorpQuestionA”);
modelBuilder.Entity().ToTable(“tbl_CorpQAnswer”);
modelBuilder.Entity().has可选(p=>p.CorpQAnswer)
.需要(x=>x.CorpQuestionA);
}

Try
modelBuilder.Entity().has可选(p=>p.CorpQAnswer)。带必填项(x=>x.CorpQuestionA)OnModelCreating
方法中,而不是
modelBuilder.Entity().haspoptional(p=>p.CorpQAnswer)。使用optionalPrincipal(x=>x.CorpQuestionA)非常感谢。如何选择你的帖子作为正确答案?我添加了评论作为答案。您可以接受:)Try
modelBuilder.Entity().hasportional(p=>p.CorpQAnswer)。WithRequired(x=>x.CorpQuestionA)OnModelCreating
方法中,而不是
modelBuilder.Entity().haspoptional(p=>p.CorpQAnswer)。使用optionalPrincipal(x=>x.CorpQuestionA)非常感谢。如何选择你的帖子作为正确答案?我添加了评论作为答案。你可以接受:)