Set the primary key for authentication in the database (SQL Server)
When using EF to insert data, I get an error
Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF
with this particular entity, although I use the same approach with other objects to insert data and no errors with them.
public class MyTable { [Key] public int ID{get;set;} public string name {get;set;} public string type {get;set;} public string status {get;set;} public int country {get;set;} public DateTime start_date {get;set;} public DateTime due_date {get;set;} }`
Request
MyTable check= new MyTable(); check.name = checkName; check.type = "DB"; check.status = "New"; check.country = country; check.start_date = DateTime.Now; check.due_date = dueDate; db.myTables.Add(check); db.SaveChanges();
Updated :
After splitting the above line
(DatabaseGenerated(DatabaseGeneratedOption.Identity))
he throws an exception
The dependent property in the reference binding maps to the store-created column. Column: 'ID'
source share