question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert error - Cannot insert explicit value for identity column in table 'YouTableName' when IDENTITY_INSERT is set to OFF.

See original GitHub issue

Cannot 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:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
henkmollemacommented, Nov 3, 2016

It should work if you remove the setter (e.g. set;) in your POCO.

0reactions
henkmollemacommented, Jan 16, 2017

Dapper.FluentMap.Dommel now supports configuring the identity of a property.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found