Code Monkey home page Code Monkey logo

Comments (2)

mikependon avatar mikependon commented on June 12, 2024

Hmmmm... A very interesting question. First, RepoDB does not understand that the underlying database IDENTITY INSERT setting has been turned ON/OFF. As RepoDB is eliminating the column in the push operations (Insert, Merge) if it is set as identity in the table, it therefore triggers that problem.

Have you tried inserting via anonymous types?

Let us say you have table like below.

CREATE TALBE [schema].[Table]
(
    [Id] INT PRIMARY IDENTITY (1, 1),
    [Name] NVARCHAR(256),
    [Description] NVARCHAR(MAX)
);

Then, you can insert all the data using the code below. (Without using any class model)

using (var connection = new SqlConnection(connectionString))
{
     connection.ExecuteNonQuery("SET IDENTITY_INSERT [schema].[Table] ON;");
     var entities = GetEntitiesAsAnonymousTypes();
     connection.InsertAll("[schema].[Table]", entities);
     connection.ExecuteNonQuery("SET IDENTITY_INSERT [schema].[Table] OFF;");
}

In which the method GetEntitiesAsAnonymousTypes() returns the list of entities as anonymous types.

public List<EntityModel> GetEntitiesAsAnonymousTypes()
{
     for (var i = 0; i <= 100; i++)
     {
          yield return new { Id = (i + 1), Name = "Name", Description = "Description };
     }
}

Note: The code above is adhocly written and is not tested, but it should work if corrected any wrong typos.

from repodb.

pmarflee avatar pmarflee commented on June 12, 2024

from repodb.

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.