Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreignkey Name error in Entity Framework Code First

When I'm using the following code, the tables are generated successfully with the Primary key and Foreign Key relations.

 [Table("tblDepartments")]
    public class DepartmentModel
    {
        [Key]
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public ICollection<EmployeeModel> Employees { get; set; }
    }



    [Table("tblEmployees")]
    public class EmployeeModel
    {
        [Key]
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }      
        public virtual DepartmentModel DID { get; set; }

    }

But when I use the following Code, I'm Getting error:

[Table("tblDepartments")]
    public class DepartmentModel
    {
        [Key]
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public ICollection<EmployeeModel> Employees { get; set; }
    }



    [Table("tblEmployees")]
    public class EmployeeModel
    {
        [Key]
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }     

        [ForeignKey("DeptID")]
        public virtual DepartmentModel DID { get; set; }

    }

ERROR:

The ForeignKeyAttribute on property 'DID' on type 'MvcApplication1.Models.EmployeeModel' is not valid. The foreign key name 'DeptID' was not found on the dependent type 'MvcApplication1.Models.EmployeeModel'. The Name value should be a comma separated list of foreign key property names.

Please Help. Thanks in advance.

like image 808
Pearl Avatar asked Jul 01 '15 11:07

Pearl


2 Answers

The problem is with your EmployeeModel as you are missing departmentid field in your table as suggested by Gert. you can use the below for EmployeeModel

[Table("tblEmployees")]
public class EmployeeModel
{
    [Key]
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }     
    public int DeptID { get; set; } //<-- You forgot to add this
    [ForeignKey("DeptID")]
    public virtual DepartmentModel DID { get; set; }

}
like image 142
Dinesh Kumar Avatar answered Nov 11 '22 17:11

Dinesh Kumar


Put the foreign key as a property inside your model then have the navigation property point to it.

public class EmployeeModel
    {
        [Key]
        public int ID { get; set; }
        public int DeptID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }     

        [ForeignKey("DeptID")]
        public virtual DepartmentModel DID { get; set; }

    }
like image 4
yohannist Avatar answered Nov 11 '22 15:11

yohannist