"The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical" 问题在 entity framework
"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 代码优先。
我正在创建 3 tables ("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”table没有错误;当我包含“Indicadores”table 时会出现问题。
知道如何解决这个问题吗?
您配置错误 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 的顺序
实际上,octavioccl 已经非常接近了。 ForeignKeyAttribute
实际上应该在导航对象上而不是 属性。
即
[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; }
其实应该是
[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; }
我正在使用 .NET Framework 4.0 Entity Framework v6 代码优先。
我正在创建 3 tables ("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”table没有错误;当我包含“Indicadores”table 时会出现问题。
知道如何解决这个问题吗?
您配置错误 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
.
实际上,octavioccl 已经非常接近了。 ForeignKeyAttribute
实际上应该在导航对象上而不是 属性。
即
[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; }
其实应该是
[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; }