“The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical” issue in entity framework

 ̄綄美尐妖づ 提交于 2021-02-07 14:14:16

问题


I'm using .NET framework 4.0 with Entity Framework v6 code-first.

I am creating 3 tables ("Indicadores", "Campos" and "Codigos") which use composite primary keys, but I am receiving an error when generating the model:

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.

The code is here:

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

Previously, I used "Campos" and "Codigos" tables with no error; The problem occurs when I include the "Indicadores" table.

Any idea of how can I solve this?


回答1:


You are configuring wrong the one-to-many relationship between Campos and Codigos. The dependent's FK must contain all columns of principal PK. Also you don't need to specify a column order in the PK of Indicadores entity, you have only one PK. Your model would be like this:

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

As you can see, I add the Nro_Serie FK property to Codigos and I change the order in the PKs of the Campos entity to match them with the order of the FKs in Codigos.




回答2:


Actually, octavioccl got pretty close. The ForeignKeyAttribute should actually be on the navigation object not the property.

I.e.

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

should actually be

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

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

[ForeignKey("Nro_compo, Nro_Serie")]
public Campos Campos { get; set; }


来源:https://stackoverflow.com/questions/28699205/the-number-of-properties-in-the-dependent-and-principal-roles-in-a-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!