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 :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…