Skip to content

Commit 7ffd397

Browse files
authored
Merge pull request #1197 from omniaintranet/master
Support int and short in SetEntityIdentities for PostGres
2 parents 89678fd + 440699c commit 7ffd397

File tree

1 file changed

+28
-1
lines changed
  • RepoDb.Extensions/RepoDb.PostgreSql.BulkOperations/RepoDb.PostgreSql.BulkOperations/Extensions

1 file changed

+28
-1
lines changed

RepoDb.Extensions/RepoDb.PostgreSql.BulkOperations/RepoDb.PostgreSql.BulkOperations/Extensions/NpgsqlHelpers.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,37 @@ private static void SetEntityIdentities<TEntity>(IEnumerable<TEntity> entities,
424424
var bulkInsertIndex = -1;
425425
var index = 0;
426426

427+
var targetPropertyType = PropertyCache.Get<TEntity>().FirstOrDefault(p => p.PropertyInfo.Name == identityField.Name);
428+
var propType = targetPropertyType.PropertyInfo.PropertyType;
429+
427430
foreach (var result in identityResults)
428431
{
429432
var entity = entityList[result.Index == bulkInsertIndex ? index : result.Index];
430-
func(entity, result.Identity);
433+
object identityValue = result.Identity;
434+
435+
//When using Return Identity operations the IdentityResult class returns the Identity as long
436+
//even though the Identity can be of type short or int so we need to convert it to the correct type
437+
if (identityValue is long longValue)
438+
{
439+
if (propType == typeof(short))
440+
{
441+
identityValue = Convert.ToInt16(longValue);
442+
}
443+
else if (propType == typeof(int))
444+
{
445+
identityValue = Convert.ToInt32(longValue);
446+
}
447+
else if (propType == typeof(short?))
448+
{
449+
identityValue = (short?)Convert.ToInt16(longValue);
450+
}
451+
else if (propType == typeof(int?))
452+
{
453+
identityValue = (int?)Convert.ToInt32(longValue);
454+
}
455+
}
456+
457+
func(entity, identityValue);
431458
index++;
432459
}
433460
}

0 commit comments

Comments
 (0)