Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

c# - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...