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

c# ef-code-first entity-framework

Question

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
5
2/24/2015 3:42:09 PM

Accepted Answer

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.

8
2/24/2015 3:43:02 PM

Popular Answer

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow