Giter Site home page Giter Site logo

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

henkmollema avatar henkmollema commented on September 1, 2024
Insert error - Cannot insert explicit value for identity column in table 'YouTableName' when IDENTITY_INSERT is set to OFF.

from dommel.

Comments (7)

henkmollema avatar henkmollema commented on September 1, 2024 1

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

from dommel.

enterpub avatar enterpub commented on September 1, 2024

Hello, I'm experiencing same issue. I have a table with Id column, which is primary key and identity,
Id bigint IDENTITY(1, 1) NOT NULL

I have a class Token, works with table Token

public sealed class Token
    {
        public long Id { get; set; }
        public string Token { get; set; }
    }

when I trying to insert new record with method:

public void CreateToken(Token token)
        {
            using (var connection = CreateConnection())
            {
                connection.Insert(token);
            }
        }

I get an the above exception. The query, generated, by Dommel looks like this:

exec sp_executesql N'set nocount on insert into Token (Id, Token) values (@Id, @Token) select cast(scope_identity() as int)',
N'@Id bigint,@Token nvarchar(4000)',
@Id=0,@Token=N'8d539f27-71fd-49b2-897d-0837d85580bbd882a1e3-90f3-46bf-8005-a51e9af65b8f'

The problem is: generated query must not contain Id column and @id parameter. It should look like this:

exec sp_executesql N'set nocount on insert into Token (Token) values (@Token) select cast(scope_identity() as int)',
N'@Token nvarchar(4000)',
@Token=N'8d539f27-71fd-49b2-897d-0837d85580bbd882a1e3-90f3-46bf-8005-a51e9af65b8f'

How to achieve such behaviour? Is there a way to say Dommel that a column is an identity column?

from dommel.

henkmollema avatar henkmollema commented on September 1, 2024

@LiveLikeLastDay @enterpub this should be fixed in v1.7.3: https://www.nuget.org/packages/Dommel/1.7.3

FYI: you can use the [Key] attribute to explicitly specify the key property for a type so it will be ignored when building queries.

from dommel.

ahoka avatar ahoka commented on September 1, 2024

Hi,

This change breaks the case when the Id actually comes from the application. This is breaking existing applications using Dommel.

from dommel.

henkmollema avatar henkmollema commented on September 1, 2024

I understand @ahoka. Although it was never the intention to set the primary key from the application side. However, I do think it might be a nice feature. I'm not sure yet how to implement both cases correctly without too much need for configuration. If someone of you have any suggestions, please share them.

from dommel.

henkmollema avatar henkmollema commented on September 1, 2024

@LiveLikeLastDay @enterpub @ahoka
I've update the IKeyPropertyResolver interface so that it indicates whether a property is a an identity. This behavior can be configured. Dapper.FluentMap.Dommel will provide support for this soon.

See a7653b0.

from dommel.

henkmollema avatar henkmollema commented on September 1, 2024

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

from dommel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.