Insert error - Cannot insert explicit value for identity column in table 'YouTableName' when IDENTITY_INSERT is set to OFF.
See original GitHub issueCannot insert explicit value for identity column in table ‘category’ when IDENTITY_INSERT is set to OFF.
private static string BuildInsertQuery(IDbConnection connection, Type type)
{
string sql;
if (!_insertQueryCache.TryGetValue(type, out sql))
{
var tableName = Resolvers.Table(type);
var keyProperty = Resolvers.KeyProperty(type);
var typeProperties = Resolvers.Properties(type)
.Where(p => p != keyProperty || keyProperty.GetSetMethod() != null)
.Where(p => p.GetSetMethod() != null)
.ToArray();
var columnNames = typeProperties.Select(Resolvers.Column).ToArray();
var paramNames = typeProperties.Select(p => "@" + p.Name).ToArray();
var builder = GetBuilder(connection);
sql = builder.BuildInsert(tableName, columnNames, paramNames, keyProperty);
_insertQueryCache[type] = sql;
}
return sql;
}
I think this line of code is a bug:
.Where(p => p != keyProperty || keyProperty.GetSetMethod() != null)
The old version works fine;
Resolvers.Properties(type).Where(p => p != keyProperty).ToList();
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (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 >Cannot insert explicit value for identity column in table when ...
Hello everyone, today morning, when I am writing code for my shopping cart project in ASP.NET CORE then I did get this error....
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 Free
Top 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
It should work if you remove the setter (e.g.
set;
) in your POCO.Dapper.FluentMap.Dommel now supports configuring the identity of a property.