首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Entity-Framework-4.1的关系和外键错误

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

Stack Overflow用户
提问于 2011-11-22 14:16:19
回答 1查看 1.9K关注 0票数 1

我有一个问题,描述如下:

代码语言:javascript
复制
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'.

我的数据库截图:

和实体定义:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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")); 
    } 
}

当我执行更新操作时,它抛出一个异常,如下所述

代码语言:javascript
复制
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; 
        } 
    }

等待帮助,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-22 16:05:07

更改共享主键,如下所示。

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8222760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档