EF Code first : Cannot insert explicit value for i

2019-06-26 12:51发布

问题:

Set the primary key to identity in database (SQL Server)

When using EF to insert data I am getting error

Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF

with this particular entity, though I am using the same approach with other entities to insert the data and getting no error with them.

   public class MyTable 
    {
    [Key/*,DatabaseGenerated(DatabaseGeneratedOption.Identity)*/]
    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;}
    }`

Query

    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 uncommenting the above line

(DatabaseGenerated(DatabaseGeneratedOption.Identity)) 

it's throwing an exception

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'

回答1:

I am not sure if it is in your case too....but the second error says..you are using your primary key as foreign key in some relation which is only valid for property having DatabaseGeneratedOption.None .

So to use your primary key as identity you can decorate it with DatabaseGeneratedOption.Identity
and in that case it will throw an exception because you can not use identity for relation(use DatabaseGeneratedOption.None instead).



回答2:

Try this:

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }


回答3:

Try this once:

objDBEntities.MyTable.AddObject(
    new tblEmployee
    {
        check.name = checkName,
        check.type = "DB",
        check.status = "New",
        check.country = country,
        check.start_date = DateTime.Now,
        check.due_date = dueDate
    }
);

objDBEntities.SaveChanges();                   

For more details : Entity Framework: How to add row data with supporting to identity column on



回答4:

Cannot insert explicit value for identity column in table 'myTable' when IDENTITY_INSERT is set to OFF

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'

It looks like you didn't define your 1....1 table relation correctly. if your pk is fk make sure you used "[Key,ForeignKey("Caller")]". I hate the way EF throw an exception. takes decays to finding out the root causes. :(

if it is not fixed your issue post both tables are related. or for root cause it is always better moving your related tables to a simple solution and tested out there so you eliminated other facts. and also make yourself more familiar with convention,fluent api and data annotation.