简体   繁体   English

实体框架中的ForeignKey声明

[英]ForeignKey declaration in Entity Framework

I have a problem with my ForeignKey. 我的ForeignKey有问题。

Error: The ForeignKeyAttribute on property 'co_art' on type 'mvcCristal.Models.Almacen' is not valid. 错误:类型'mvcCristal.Models.Almacen'上属性'co_art'上的ForeignKeyAttribute无效。 The navigation property 'Articulo' was not found on the dependent type 'mvcCristal.Models.Almacen'. 在从属类型“ mvcCristal.Models.Almacen”上找不到导航属性“ Articulo”。 The Name value should be a valid navigation property name. Name值应为有效的导航属性名称。

My Classes Declaration: 我的课程声明:

public class Articulo
{
    [Required]
    public string co_art { get; set; }
    public string des_art { get; set; }
    public string modelo { get; set; }    
    public string co_lin { get; set; }
    public string co_subl { get; set; }
    public string co_cat { get; set; }
    public string co_col { get; set; }
    [ForeignKey("co_art")]
    public ICollection<Almacen> Almacenes { get; set; }
}

public class Almacen
{
    [Key, Column(Order = 0)]
    public string co_alma { get; set; }
    public string des_alm { get; set; }
    [Required, ForeignKey("Articulo"), Column(Order = 1)]
    public string co_art { get; set; }
    public double stock_act { get; set; }
}

Any help? 有什么帮助吗? Thanks; 谢谢; I am new in EF. 我是EF的新手。

Your annotations looks incorrect 您的注释看起来不正确

It seems you want to associate a one-to-many relationship using annotations. 似乎您想使用批注关联一对多关系。

Articulo 1 <--> * Almacen

Keys and Foreign keys should be of type int. 键和外键应为int类型。

I recommend follow a tutorial for more about annotations, something like these 我建议您按照教程中的内容了解更多有关注释的信息

You can also use fluent api instead of annotations 您还可以使用流利的api代替注释

The exception that you're getting should be pointing you in the right direction as it's explicitly stating the problem. 您所获得的异常应该将您指向正确的方向,因为它明确说明了问题。 It says, "The navigation property 'Articulo' was not found on ... '...Almacen'. The Name value should be a valid navigation property name." 它说:“在...'... Almacen上找不到导航属性'Articulo'。Name值应该是有效的导航属性名称。”

Okay, so the Name value of the foreign key (ie Articulo ) needs to be a navigation property on Almacen , so let's do that. 好的,因此外键(即Articulo )的Name值需要是Almacen上的导航属性,所以让我们这样做。

public class Almacen
{
    [Key, Column( Order = 0 )]
    public string co_alma { get; set; }
    public string des_alm { get; set; }
    [Required, ForeignKey( "Articulo" ), Column( Order = 1 )]
    public string co_art { get; set; }
    public double stock_act { get; set; }
    public Articulo Articulo { get; set; }
}

After doing that I got another error saying that co_art needed to be a key on Articulo , so I added that. 完成此操作后,我又遇到另一个错误,说co_art必须是Articulo的键,因此我添加了它。

public class Articulo
{
    [Required]
    [Key]
    public string co_art { get; set; }
    public string des_art { get; set; }
    public string modelo { get; set; }
    public string co_lin { get; set; }
    public string co_subl { get; set; }
    public string co_cat { get; set; }
    public string co_col { get; set; }
    [ForeignKey( "co_art" )]
    public ICollection<Almacen> Almacenes { get; set; }
}

At that point everything seemed happy. 那时一切似乎都很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM