mikependon / RepoDB

A hybrid ORM library for .NET.
Apache License 2.0
1.68k stars 122 forks source link

Bug: InsertAll on MSSQL (returns wrong Identifier) #1129

Open Garios opened 1 year ago

Garios commented 1 year ago

Bug Description

We have a table:

Without setting the identifier, it generates the following SQL-Command:

INSERT INTO [Table] ( [ClientID], [ID], [Value]) VALUES ( @ClientID, @ID, @Value ) ;

SELECT CONVERT(BIGINT, @ClientID) AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;

The SQL-Command doesn't change, if we set the identifier to ID by:

FluentMapper.Entity<ManagedLogbooks_Types>().Identity(p => p.ID);

Therefore it always returns the value from ClientID:

InsertAll.cs on line 849

do
{
    if (await reader.ReadAsync(cancellationToken))
    {
        // No need to use async on this level (await reader.GetFieldValueAsync<object>(0, cancellationToken))
        var value = Converter.DbNullToNull(reader.GetValue(0));  //!!!!!!!!!!!!!!!!! here
        var index = batchItems.Count > 1 && reader.FieldCount > 1 ? reader.GetInt32(1) : position;
        context.KeyPropertySetterFunc.Invoke(batchItems[index], value);
        result++;
    }
    position++;
}
while (await reader.NextResultAsync(cancellationToken));

Next problem

Assume it returns the correct Field-Value (ID). ID in our special case is an enumeration. In this case it throws an invalid cast exceptionr on the following line:

context.KeyPropertySetterFunc.Invoke(batchItems[index], value);

Message:
Invalid cast from 'System.Int64' to '[EnumerationType]'. 

StackTrace:
   bei System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   bei RepoDb.Converter.ToType[T](Object value)
   bei RepoDb.DbConnectionExtension.<InsertAllAsyncInternalBase>d__364`1.MoveNext()

Library Version:

Example: current master

mikependon commented 1 year ago

Thanks for reporting this issue, we will include the fix to this issue in the next release.