Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi to all,
Here i want to set foregin key for employee id but while running code first migration i got a error.How to resolve it.

MODEL DESCRIPTION:-

C#
[Required]
[ForeignKey("Employee_Id")]
public int Employee_Id { get; set; }




ERROR DESCRIPTION:-
The property 'Employee_Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<t> where T is a valid entity type.
Posted

C#
[Required]
[ForeignKey("TableName")]
public int Employee_Id { get; set; }


use tablename inside ForeignKey("----")
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 1-Aug-14 7:46am    
The ForeignKeyAttribute on property 'Employee_Id' on type 'Tata.BusinessEntities.Entities.User' is not valid. The navigation property 'tblUser' was not found on the dependent type 'Tata.BusinessEntities.Entities.User'. The Name value should be a valid navigation property name.
Now i got this error
Kunjammu 1-Aug-14 7:49am    
Did u set foriegn key in table( i mean in the database)
JOTHI KUMAR Member 10918227 1-Aug-14 7:56am    
hey table not created yet bcz i'm using entity frame work so table was created automatically
Kunjammu 1-Aug-14 8:12am    
please try in reverse way. then it will work
JOTHI KUMAR Member 10918227 1-Aug-14 8:18am    
ok wait i will try and tell to you ok
public class Customer
{
[Key]
public int CustomerId { get; set; }

[StringLength(20)]
[Required]
public string CustomerName { get; set; }

}

public class Address
{
[Key]
public int AddressId { get; set; }

[DataType(DataType.Text)]
public string Street { get; set; }



//Foreign Key
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }

}
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 1-Aug-14 8:27am    
where u defined customer in entities or dbset
u can define customer model in dbset as fellow

public class LocalContext : DbContext
{
public LocalContext() : base("test")
{

//Database.SetInitializer<LocalContext>(null);
}

public DbSet<Customer> Customers { get; set; }
public DbSet<Address> Addresses { get; set; }

}
 
Share this answer
 
Comments
Nirav Prabtani 1-Aug-14 9:16am    
Dont post more than one answer, just edit answer if you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900