“关系约束中的从属角色和主要角色中的属性数必须相同".实体框架中的问题 [英] "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical" issue in entity framework
问题描述
我正在使用.NET Framework 4.0和Entity Framework v6代码优先.
I'm using .NET framework 4.0 with Entity Framework v6 code-first.
我正在创建3个使用复合主键的表(" Indicadores "," Campos "和" Codigos "),但是我生成模型时收到错误:
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::关系中从属角色和主要角色的属性约束必须相同.
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 "表时,会发生此问题.
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?
推荐答案
您正在错误地配置 Campos
和 Codigos
之间的一对多关系.受抚养人的FK必须包含主体PK的所有列.另外,您无需在 Indicadores
实体的PK中指定列顺序,因为只有一个PK.您的模型将如下所示:
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; }
}
如您所见,我将 Nro_Serie
FK属性添加到 Codigos
,然后将 Campos
实体的PK的顺序更改为将它们与 Codigos
中的FK的顺序匹配.
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
.
这篇关于“关系约束中的从属角色和主要角色中的属性数必须相同".实体框架中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!