ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – “关系约束中的从属角色和主要角色中的属性数量必须相同”实体框架中的问题

2019-06-12 03:52:20  阅读:111  来源: 互联网

标签:c entity-framework ef-code-first


我正在使用.NET Framework 4.0和Entity Framework v6代码优先.

我正在创建使用复合主键的3个表(“Indicadores”,“Campos”和“Codigos”),但是在生成模型时收到错误:

One or more validation errors were detected during model generation:

Codigos_Campos_Target_Codigos_Campos_Source: : The number of
properties in the Dependent and Principal Roles in a relationship
constraint must be identical.

代码在这里:

   

public class Indicadores
{
    [Key, Column(Order = 0)]
    public Int32 Nro_Serie { get; set; }

    [MaxLength(31)]
    public string Nombre { get; set; }

    public List<Campos> campo { get; set; }
}

public class Codigos
{
    [Key, Column(Order = 0), DataType("nvarchar"), MaxLength(31)]
    public string Codigo {get;set;}

    [MaxLength(31)]
    public string Descripcion1 {get;set;}

    [MaxLength(31)]
    public string Descripcion2 {get;set;}

    public Int32 CantidadPesadas {get;set;}

    public Int32 PesoTotal {get;set;}

    [Key, Column(Order = 1)]
    public Int16 Nro_Campo { get; set; }

    [ForeignKey("Nro_Campo")]
    public Campos Campos { get; set; }    
}


public class Campos
{
    [Key, Column(Order = 0), DataType("smallint"), DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
    public Int16 Nro_Campo {get;set;}

    [Required]
    public string Nombre {get;set;}

    public List<Codigos> codigo { get; set; }

    [Key, Column(Order = 1)]
    public Int32 Nro_Serie { get; set; }

    [ForeignKey("Nro_Serie")]
    public Indicadores Indicadores { get; set; }
} 

以前,我使用“Campos”和“Codigos”表没有错误;当我包含“Indicadores”表时会出现问题.

知道怎么解决这个问题?

解决方法:

您正在配置错误Campos和Codigos之间的一对多关系.从属的FK必须包含主PK的所有列.此外,您不需要在Indicadores实体的PK中指定列顺序,您只有一个PK.你的模型是这样的:

public class Indicadores
{
    [Key]
    public Int32 Nro_Serie { get; set; }
    [MaxLength(31)]
    public string Nombre { get; set; }
    public List<Campos> campo { get; set; }
}

public class Codigos
{
    [Key]
    [Column(Order = 0)]
    [DataType("nvarchar")]
    [MaxLength(31)]
    public string Codigo { get; set; }
    [MaxLength(31)]
    public string Descripcion1 { get; set; }
    [MaxLength(31)]
    public string Descripcion2 { get; set; }
    public int CantidadPesadas { get; set; }
    public int PesoTotal { get; set; }

    [Key,ForeignKey("Campos"),Column(Order = 1)]
    public Int16 Nro_Campo { get; set; }

    [ForeignKey("Campos"), Column(Order = 2)]
    public Int32 Nro_Serie { get; set; }

    public Campos Campos { get; set; }
}

public class Campos
{
    [Key, Column(Order = 1)]
    [DataType("smallint")]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
    public Int16 Nro_Campo { get; set; }
    [Required]
    public string Nombre { get; set; }
    public List<Codigos> codigo { get; set; }

    [Key, Column(Order = 2)]
    public Int32 Nro_Serie { get; set; }

    [ForeignKey("Nro_Serie")]
    public Indicadores Indicadores { get; set; }
} 

如您所见,我将Nro_Serie FK属性添加到Codigos,我更改了Campos实体的PK中的顺序,以使它们与Codigos中的FK顺序相匹配.

标签:c,entity-framework,ef-code-first
来源: https://codeday.me/bug/20190612/1223329.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有