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

时间:2011-11-22 06:16:19

标签: 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_CorpQAnswer'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

我的数据库是屏幕截图: http://i.6.cn/cvbnm/18/e0/23/da82be1ab8adfcd84f68d1a46d645e97.jpg

并且实体定义:

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

当我更新操作时,它会抛出异常作为

之前的描述
public bool AnswerCorpQuestion(AnswerCorpQuestionModel acqModel) 
    { 
        var prjPO = new ProjectDataEntities(); 

        //update table CorpQuestionA  
        CorpQuestionA cqaModel0 = prjPO.Tbl_CorpQuestionAs.Find(acqModel.cqua_QuestionId); 
        cqaModel0.cqua_IsAnswer = acqModel.cqua_IsAnswer; 

        //insert table CorpQAnswer  
        CorpQAnswer cqaModel1 = new CorpQAnswer 
        { 
            cqan_QuestionId = acqModel.cqua_QuestionId, 
            cqan_Answer=acqModel.cqan_Answer 
        }; 
        prjPO.Tbl_CorpQAnswers.Add(cqaModel1); 

        try 
        { 
            prjPO.SaveChanges(); 
            return true; 
        } 
        catch(DbEntityValidationException dbEx) 
        { 
            throw dbEx; 
        } 
    }

等待帮助,thx

1 个答案:

答案 0 :(得分:1)

按如下方式更改共享主键。

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);
}