Cannot insert explicit value for identity column in table 'TABLE' when IDENTITY_INSERT is set to OFF
See original GitHub issueI 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:
{"Cannot insert explicit value for identity column in table 'ITEMS' when IDENTITY_INSERT is set to OFF."}
My simplified service code:
public Item AddItem(Item item)
{
db.Items.Add(item);
if(db.SaveChanges() > 0)
{
return item;
}
return null;
}
}
My OnModelCreating override:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Items>()
.Key(e => e.Id);
builder.Entity<Items>(e =>
{
e.Metadata.SqlServer().Table = "ITEMS";
e.Property(c => c.Id).Metadata.SqlServer().Column = "ITEM_ID";
e.Property(c => c.Id).Metadata.SqlServer().ColumnType = "numeric(18,0)";
e.Property(c => c.Id).GenerateValueOnAdd(true);
e.Property(c => c.Name).Metadata.SqlServer().Column = "ITEM_NAME";
e.Property(c => c.Name).MaxLength(50).Required();
e.Property(c => c.LastModified).Metadata.SqlServer().Column = "LAST_MODIFIED_DTM";
});
}
And lastly, the SQL Command that is being executed against SqlServer:
--The data may be truncated and may not represent the query that was run on the server
USE [database];
GO
--Type and value data was not available for the following variables. Their values have been set to defaults.
DECLARE @p0 AS SQL_VARIANT;
DECLARE @p1 AS SQL_VARIANT;
DECLARE @p2 AS SQL_VARIANT;
SET @p0 = NULL;
SET @p1 = NULL;
SET @p2 = NULL;
SET NOCOUNT OFF;
INSERT INTO [ITEMS] ([ITEM_ID], [LAST_MODIFIED_DTM], [ITEM_NAME])
VALUES (@p0, @p1, @p2);
SELECT @@ROWCOUNT;
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?
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Cannot insert explicit value for identity column in table ' ...
The PRIMARY KEY "ID" MUST BE PRESENT from the "INSERT INTO" Statements as long as the ID value does not already exist: If...
Read more >How to Fix 'Can't insert explicit value for identity column in ...
Error 1: Set identity_insert OFF ... In the first case, we will insert data into the table with the “IDENTITY INSERT” set to...
Read more >Execute Non Query: Cannot insert explicit value for identity ...
Execute Non Query: Cannot insert explicit value for identity column in table 'Sample_Table_Name' when IDENTITY_INSERT is set to OFF.
Read more >Little SQL Server Tricks: How to Fix "Cannot insert explicit ...
In this case, your INSERT statement fails because IDENTITY_INSERT is set to OFF. You need to change it to ON, insert your rows...
Read more >SQL Server Error Messages - Msg 544
SQL Server Error Messages - Msg 544 - Cannot insert explicit value for identity column in table <Table Name> when IDENTITY_INSERT is set...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@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?