Pearl
Pearl

Reputation: 9445

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.

Upvotes: 8

Views: 10870

Answers (3)

Dinesh Kumar
Dinesh Kumar

Reputation: 186

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; }

}

Upvotes: 10

bubi
bubi

Reputation: 6501

In '[ForeignKey("DeptID")]' you need to have the property DeptID in the model. If you don't want it but just the name DeptID on the foreign key field you need to use fluent interface to configure the relationship i.e.

        HasOptional(t => t.DID)
            .WithMany()
            .Map(d => d.MapKey("DeptID"));

Upvotes: 0

yohannist
yohannist

Reputation: 4204

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; }

    }

Upvotes: 4

Related Questions