-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Cannot insert explicit value for identity column in table 'TABLE' when IDENTITY_INSERT is set to OFF #2307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Here's the stack trace for reference:
|
@scottengle what is the value assigned to |
The item ID is a (non-nullable) decimal type and the value defaults to 0.0. I'm basically testing this through an API call from a REST client in the browser and I'm sending only the Name and LastModified properties in the request. |
Here's the Web API implementation for POST, but this errors out in the call to AddItem():
|
@scottengle we're having trouble getting to the bottom of this since the decimal column should never be configured as Identity by convention. Could it be that you mapping to an existing database that was not created by EF? If so, you would need to configure that property as Identity (since it won't be configured that way by convention). modelBuilder.Entity<Items>()
.Property(i => i.Id)
.ForSqlServer()
.UseIdentity() |
BTW I notice you are using the Metadata property a lot in your model builder code. Any particular reason you use Was it just a matter of finding the APIs first, or was there a reason you prefer the Metadata one? |
@rowanmiller Yes, I'm mapping to an existing database that wasn't created by EF originally. I'll try assigning the Identity. With respect to the Metadata calls, I started working on this code base during beta3 and there wasn't much documentation at the time on how to code up a "database first" project. I think the Metadata calls just ended up working for me at the time. If the ForRelational calls are better practice, I'll definitely use those. |
@scottengle ok that makes sense. If EF doesn't know that the column is IDENTITY then it will try and insert values for you. Here are some very early docs on using the configuration API http://ef.readthedocs.org/en/latest/modeling/configuring.html. |
Hi rowan, |
I am experiencing this issue as well now with EF7 my object has a auto-incremented int primary key but whenever I try to insert EF does not try to generate a key for the object.
|
@carnag3kid7 please open a new issue with details of what you are seeing. The issue being tracked here is closed. |
@scottengle , have not investigated this, but that e.Property(c => c.Id).GenerateValueOnAdd(true); is probably only gonna work with Identity Insert 'ON', else what will generate the ID? |
@scottengle You can generate your own Id, this will be an issue on a multi user system, too keep it in sync, actually kind of impossible, in that case u can auto generate a guid as your Id, but this is dirty on a central db. |
I have an existing database table that doesn't allow identity inserts. When I try to add a new item to the database, I get the following error message:
My simplified service code:
My OnModelCreating override:
And lastly, the SQL Command that is being executed against SqlServer:
Because Identity Insert is turned off, I can't have the Item ID included when an insert occurs. Is there a proper way to deal with this scenario?
The text was updated successfully, but these errors were encountered: