locked
ef 4.1 code-first : Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF. RRS feed

  • Question

  • In my database, I have an Employees table that has columns
    RowNumber - type int, identity column
    EmployeeId - type nvarchar, primary key.

    This maps into an Employee model class

    public partial class Employee
    {
     public int RowNumber {get;set;}
     public string EmployeeId {get;set;}
    }
    

    Since this is a code-first setup, I have no .edmx file.

    When the program runs, a user types in a new employee id and clicks the Create button.

    When this piece of code executes

    if ( ModelState.IsValid )
    {
     this.objectContext.Employees.Add(employee);
     this.objectContext.SaveChanges(); // this generates an error
    
     return RedirectToAction("Index");
    }
    

    I get an error message saying
    Cannot insert explicit value for identity column in table Employees when IDENTITY_INSERT is set to OFF.

    Can anybody shed light on this?

    Tuesday, March 22, 2011 12:13 AM

Answers

All replies