zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
383 stars 85 forks source link

Experiencing SQLite NOT NULL constraint failed on Primary KEY #65

Closed koskedk closed 4 years ago

koskedk commented 4 years ago

Hi I recently upgraded to Dapper plus 3.0.15 from 3.0.5 in my .Net Core 3.1 project.

I'm currently having problems when saving a new object into an SQLite database: this is the error

_Microsoft.Data.Sqlite.SqliteException : SQLite Error 19: 'NOT NULL constraint failed: Car.Id'. at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteDataReader.NextResult() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery() at .(DbCommand , BulkOperation , Int32 ) at .( , DbCommand ) at .Execute(List1 actions) at .(List1 ) at Z.BulkOperations.BulkOperation.Execute() at Z.BulkOperations.BulkOperation.BulkInsert() at Z.Dapper.Plus.DapperPlusAction.Execute() at Z.Dapper.Plus.DapperPlusActionSet1.DapperPlusActionSetBuilder(DapperPlusContext context, IDbConnection connection, IDbTransaction transaction, String mapperKey, DapperPlusActionKind actionKind, IEnumerable1 items) at Z.Dapper.Plus.DapperPlusActionSet1..ctor(DapperPlusContext context, IDbConnection connection, String mapperKey, DapperPlusActionKind actionKind, IEnumerable1 items) at Z.Dapper.Plus.DapperPlusExtensions.BulkInsert[T](IDbConnection connection, String mapperKey, T[] items) at Z.Dapper.Plus.DapperPlusExtensions.BulkInsert[T](IDbConnection connection, T[] items)_

Here is a sample code

using System;
using Microsoft.Data.Sqlite;
using Z.Dapper.Plus;

namespace Sample
{
    public class Demo
    {
        public Demo()
        {
            DapperPlusManager.Entity<Car>()
                .Table(nameof(Car))
                .Map(m => m.Id, nameof(Car.Id))
                .Map(m => m.Name, nameof(Car.Name))
                .Key(x => x.Id);
        }

        public void SaveNewCar()
        {
            var car = new Car("VelarX");
            using (var cn = new SqliteConnection("test/Sample/garage.db"))
            {
                cn.BulkInsert(car);
            }
        }
    }

    public class Car
    {
        public Guid Id { get; set; }
        public string Name { get; set; }

        public Car(string name)
        {
            Id = Guid.NewGuid();
            Name = name;
        }
    }
}

I have had to downgrade to Dapper plus 3.0.5 for it to work

JonathanMagnan commented 4 years ago

Hello @koskedk ,

Thank you for the project, we will try it and check why that's happening.

Best Regards,

Jon

JonathanMagnan commented 4 years ago

Hello @koskedk ,

The v3.0.17 has been released.

Could you try it and let us know if everything is now working as expected?

We indeed, unfortunately, introduced this issue recently.

koskedk commented 4 years ago

I can confirm that it's now working. Thank you