[ASP.NET MVC] The ForeignKeyAttribute on property is not valid


You might encounter the error (below) when working on ASP.NET MVC project stating that :

The ForeignKeyAttribute on property 'EmployeeId' on type 'EmployeeSalary' 
is not valid. The navigation property 'Employee' was not found on the dependent 
type 'EmployeeSalary'. The Name value should be a valid navigation property name.

This error occurred when you trying to set ForeignKey attribute without indicating navigate
property name.

Here our EmployeeSalary table :
public partial class EmployeeSalary 
 {
 [Key]
 public int id { get; set; }
 [ForeignKey("Employee")]
 public int EmployeeId { get; set; }
 public decimal Salary{ get; set; }
 }

As you might have already seen we didn’t assign navigate property name,therefore,the error above occurred.In order to solve this problem we should just add property as this:

public virtual Employee Employee { get; set; }

Tagged:

Cavab yaz

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: