gpt4 book ai didi

c# - Entity Framework 中的 "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical"问题

转载 作者:太空狗 更新时间:2023-10-29 22:36:56 24 4
gpt4 key购买 nike

我将 .NET Framework 4.0 与 Entity Framework v6 代码优先结合使用。

我正在创建 3 个使用复合主键的表(“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”表没有错误;当我包含“Indicadores”表时出现问题。

知道如何解决这个问题吗?

最佳答案

您配置错误的 CamposCodigos 之间的一对多关系。受抚养人的 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 的顺序相匹配。

关于c# - Entity Framework 中的 "The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com