Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;关系约束中从属角色和主要角色中的属性数必须相同;实体框架中的问题_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# &引用;关系约束中从属角色和主要角色中的属性数必须相同;实体框架中的问题

C# &引用;关系约束中从属角色和主要角色中的属性数必须相同;实体框架中的问题,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我首先将.NET framework 4.0与Entity framework v6代码一起使用 我正在创建3个使用复合主键的表(“索引””、“Campos”和“Codigos”),但生成模型时收到错误: 在模型生成过程中检测到一个或多个验证错误: Codigos_Campos_Target_Codigos_Campos_Source:: 关系中从属角色和主要角色中的属性 约束必须相同 代码如下: public class Indicadores { [Key, Column(Order

我首先将.NET framework 4.0与Entity framework v6代码一起使用

我正在创建3个使用复合主键的表(“索引””、“Campos”和“Codigos”),但生成模型时收到错误:

在模型生成过程中检测到一个或多个验证错误:

Codigos_Campos_Target_Codigos_Campos_Source:: 关系中从属角色和主要角色中的属性 约束必须相同

代码如下:

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; }
} 
公共类指示符
{
[键,列(顺序=0)]
public Int32 Nro_Serie{get;set;}
[MaxLength(31)]
公共字符串Nombre{get;set;}
公共列表campo{get;set;}
}
公共类Codigos
{
[键,列(顺序=0),数据类型(“nvarchar”),最大长度(31)]
公共字符串Codigo{get;set;}
[MaxLength(31)]
公共字符串description1{get;set;}
[MaxLength(31)]
公共字符串description2{get;set;}
公共Int32 CantidadPesadas{get;set;}
公共Int32伪目标{get;set;}
[键,列(顺序=1)]
公共Int16 Nro_Campo{get;set;}
[外侨(“诺乌坎波”)]
公共Campos Campos{get;set;}
}
公共类露营车
{
[Key,Column(Order=0),DataType(“smallint”),DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
公共Int16 Nro_Campo{get;set;}
[必需]
公共字符串Nombre{get;set;}
公共列表codigo{get;set;}
[键,列(顺序=1)]
public Int32 Nro_Serie{get;set;}
[外汇(“Nro_系列”)]
公共指示符指示符{get;set;}
} 

以前,我使用了“Campos”和“Codigos”表,没有错误;当我包含“指示符”表时,就会出现问题


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

你在配置
Campos
Codigos
之间的一对多关系时出错了。依赖项的FK必须包含主体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; }
} 
公共类指示符
{
[关键]
public Int32 Nro_Serie{get;set;}
[MaxLength(31)]
公共字符串Nombre{get;set;}
公共列表campo{get;set;}
}
公共类Codigos
{
[关键]
[第列(顺序=0)]
[数据类型(“nvarchar”)]
[MaxLength(31)]
公共字符串Codigo{get;set;}
[MaxLength(31)]
公共字符串description1{get;set;}
[MaxLength(31)]
公共字符串description2{get;set;}
公共int CantidadPesadas{get;set;}
public int PesoTotal{get;set;}
[键,外键(“Campos”),列(顺序=1)]
公共Int16 Nro_Campo{get;set;}
[ForeignKey(“Campos”),第列(顺序=2)]
public Int32 Nro_Serie{get;set;}
公共Campos Campos{get;set;}
}
公共类露营车
{
[键,列(顺序=1)]
[数据类型(“smallint”)]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGenerateOption.None)]
公共Int16 Nro_Campo{get;set;}
[必需]
公共字符串Nombre{get;set;}
公共列表codigo{get;set;}
[键,列(顺序=2)]
public Int32 Nro_Serie{get;set;}
[外汇(“Nro_系列”)]
公共指示符指示符{get;set;}
} 

如您所见,我将
Nro_Serie
FK属性添加到
Codigos
中,并更改
Campos
实体的PKs中的顺序,以使其与
Codigos
中的FKs顺序相匹配实际上,octavioccl非常接近。
ForeignKeyAttribute
实际上应该位于导航对象上,而不是属性上

实际上应该是

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

可能是重复的谢谢你的帮助!我不知道我必须在principal PK上有所有列。indicators中的列顺序是以前测试的垃圾。。
[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; }