Sachin
Sachin

Reputation: 11

Validation error during model generation

I get the following error when generating a model:

One or more validation errors were detected during model generation: MvcDemo.Models.Employee: : EntityType 'Employee' has no key defined.

How would I solve this?

[Table("tblEmployee")] 
    public class Employee
    {
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }

Error:

Line 15:  {
Line 16:    EmployeeContext employeeContext = new EmployeeContext();
Line 17:    Employee employee = employeeContext.Employees.Single(emp => emp.EmpID == id);
Line 18:             
Line 19:    return View(employee);

Upvotes: 1

Views: 5967

Answers (1)

markpsmith
markpsmith

Reputation: 4918

Either change your Primary Key from EmpId to Id or add an annotation to your current key:

[Key]
public int EmpID { get; set; }

I've made assumptions that this is Entity Framework Code First btw...

Making one of these changes will tell EF to automatically create your key as an identity column.

Upvotes: 6

Related Questions