简体   繁体   中英

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."

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. 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.

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:

[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.

Hope this solves your issue, let me know if you have any further questions:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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