Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
One or more validation errors were detected during model generation:

mvcstarted.Models.employee: : EntityType 'employee' has no key defined. Define the key for this EntityType.
employees: EntityType: EntitySet;employees; is based on type;employee; that has no keys defined.


public ActionResult Details(int id)
{
employeecontext empt = new employeecontext();
employee emptt = empt.employees.Single(emp => emp.Emp_id== id);

return View(emptt);
}

What I have tried:

i have tried so much time until now but issue still arrising as above please suggest me what can i do further
Posted
Updated 4-Aug-21 1:06am
Comments
phil.o 23-Aug-16 4:34am    
Did you try to give a primary key to your Employee table?
jay49 23-Aug-16 5:19am    
how can solve this pls suggest me ...still until nw this problem arrised hw can solve it
jay49 23-Aug-16 5:21am    
i have select (Emp_id) as a primary key in sql server 2016..so what does
phil.o 23-Aug-16 5:41am    
There is a delta between what you tell me (Employee table has a primary key) and the message you get (entity has no primary key).
Did you try to update your datasource to reflect most recent changes to your entities?
jay49 23-Aug-16 5:50am    
i have done also it update that datasource but still problem arrised

You need to do two things(this is related to code first approach):

1- Determine the primary key
[Key]
public int BidYearID { get; set; }

2- you have to build your solution then try to add the view
 
Share this answer
 
Comments
CHill60 1-Mar-17 11:57am    
Doesn't add anything to the answer provided 6 months ago
Yahya Mohammed Ammouri 1-Mar-17 23:51pm    
The new addition is you have to build the project after adding the [key] attribute.

This is what I faced myself; I have Data classes in separate DAL project and it didn't work until I re-build the DAL project.
I was facing this error because of column name in table and properties name in the entity class were not matching.Ensure that table name is either matching or you are using table attribute in case table name and entity class name is not matching. Database name should match too I guess not sure abot that..
 
Share this answer
 

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