简体   繁体   English

代码优先 类型 '' 的属性 '' 上的 ForeignKeyAttribute 无效。 实体框架

[英]Code First The ForeignKeyAttribute on property '' on type '' is not valid. Entity Framework

I am trying to create code first approach a user table which have different foreign keys,but I it gives me error with this message:我正在尝试创建代码首先接近具有不同外键的用户表,但它给了我这条消息的错误:

"The ForeignKeyAttribute on property 'discount_id' on type 'EFNetflixAssignment.User' is not valid. The foreign key name 'Discounts' was not found on the dependent type 'EFNetflixAssignment.Discount'. The Name value should be a comma separated list of foreign key property names." “类型‘EFNetflixAssignment.User’的属性‘discount_id’的ForeignKeyAttribute无效。在依赖类型‘EFNetflixAssignment.Discount’上找不到外键名称‘Discounts’。名称值应该是逗号分隔的外键列表关键属性名称。”

Here is my code for the user table这是我的用户表代码

{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Required]
    public virtual int Id { get; set; }

    [Required]
    public virtual bool Status { get; set; }

    [Required]
    [MaxLength(255)]
    public virtual string Email { get; set; }

    [Required]
    [MaxLength(255)]
    public virtual string Password { get; set; }

    [Required]
    public virtual List<Payment_type> Payment_Types { get; set; }

    [Required]
    public virtual bool Activated { get; set; }

    [Required]
    [ForeignKey("Discounts")]
    public virtual List<Discount> discount_id { get; set; }
}

And here is the discount code这是折扣码

public class Discount
{
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Required]
        public virtual int Id { get; set; }

        [Required]
        [MaxLength(255)]
        public virtual string discount_type { get; set; }

        [Required]
        public virtual decimal discount_amount { get; set; }
}

Can somebody help me solve this issue?有人可以帮我解决这个问题吗?

The ForeignKey-Attribute won't work like this. ForeignKey-Attribute 不会像这样工作。 If you don't care about the name of the foreign key in the database, then you don't need this attribute.如果您不关心数据库中外键的名称,则不需要此属性。 EF will automatically create a foreign key for the navigation properties. EF 将自动为导航属性创建外键。

If you want to specify the name of this key, then you need another property in the user class, that holds the foreign key and then you can connect these 2 properties in 2 possible ways:如果要指定此键的名称,则需要用户 class 中的另一个属性,该属性包含外键,然后您可以通过两种可能的方式连接这两个属性:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }

[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]

public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }

[Required]
public virtual List<Payment_type> Payment_Types { get; set; }

[Required]
public virtual bool Activated { get; set; }

[Required]
[ForeignKey("Discounts")]
public List<int> Discount_Ids { get; set; }

[Required]
public virtual List<Discount> Discounts { get; set; }

Or like this:或者像这样:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public virtual int Id { get; set; }

[Required]
public virtual bool Status { get; set; }
[Required]
[MaxLength(255)]

public virtual string Email { get; set; }
[Required]
[MaxLength(255)]
public virtual string Password { get; set; }

[Required]
public virtual List<Payment_type> Payment_Types { get; set; }

[Required]
public virtual bool Activated { get; set; }

[Required]
public List<int> Discount_Ids { get; set; }

[Required]
[ForeignKey("Discount_Ids")]
public virtual List<Discount> Discounts { get; set; }

This is because whenever you want to set a specific name for a foreign key using Data Annotations you need to add a property for that key and connect it with the navigation property.这是因为每当您想使用数据注释为外键设置特定名称时,您都需要为该键添加一个属性并将其与导航属性连接起来。

But be aware of the fact, that on your database the foreign key will be set in the discount table, because in a 1-to-many relationship the table on the many-side always takes the primary key of the 1-side as the foreign key for the relationship.但请注意,在您的数据库中,外键将设置在折扣表中,因为在一对多关系中,多方的表总是将 1 方的主键作为关系的外键。

Hope this solves your issue, let me know if you have any further questions:)希望这能解决您的问题,如果您还有其他问题,请告诉我:)

暂无
暂无

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

相关问题 首先进行编码,属性“”上类型“”的ForeignKeyAttribute无效。 EF 6.1.3 - Code First The ForeignKeyAttribute on property '' on type '' is not valid. EF 6.1.3 实体框架ForeignKeyAttribute在属性…在类型…上无效 - Entity framework ForeignKeyAttribute on property … on type … is not valid 代码优先方法中使用Entity Framework的ForeignKeyAttribute名称无效 - Invalid ForeignKeyAttribute name with Entity Framework in code first approach 类型 &#39;Class1&#39; 上的属性 &#39;MyField&#39; 上的 ForeignKeyAttribute 无效 - The ForeignKeyAttribute on property 'MyField' on type 'Class1' is not valid 类型“[项目名称]”上的属性“[模型名称]”上的 ForeignKeyAttribute 无效 - The ForeignKeyAttribute on property "[Model name]" on type "[Project name]" is not valid 类型“ Equipment.Models.Device”上属性“ TypeId”上的ForeignKeyAttribute无效 - The ForeignKeyAttribute on property 'TypeId' on type 'Equipment.Models.Device' is not valid 实体框架代码首先更新复杂类型的单个属性 - Entity Framework Code First Update a Single Property of a Complex Type 实体框架核心 ForeignKeyAttribute 两边 - Entity Framework Core ForeignKeyAttribute on both sides 实体框架优先:如何附加实体并修改集合类型属性? - Entity Framework Code-First: How to attach an entity and modify collection type property? 实体框架代码优先-确定数据类型 - Entity Framework Code First - determining data type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM