Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 948
aquiles Avatar asked Feb 24 '15 15:02

aquiles


1 Answers

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.

like image 125
octavioccl Avatar answered Oct 19 '22 14:10

octavioccl