diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs index d685a3f..15bf2fa 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs index 0251ea4..1975950 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs @@ -48,6 +48,7 @@ public static UpsertCommandBuilder UpsertRange(this DbContext throw new ArgumentNullException(nameof(entities)); var entityType = dbContext.GetService().FindEntityType(typeof(TEntity)) + ?? (entities.Length == 0 ? null : dbContext.GetService().FindEntityType(entities.First().GetType())) ?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext); return new UpsertCommandBuilder(dbContext, entityType, entities); } @@ -67,13 +68,14 @@ public static UpsertCommandBuilder UpsertRange(this DbContext if (entities == null) throw new ArgumentNullException(nameof(entities)); - var entityType = dbContext.GetService().FindEntityType(typeof(TEntity)) - ?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext); var collection = entities switch { ICollection entityCollection => entityCollection, _ => entities.ToArray() }; + var entityType = dbContext.GetService().FindEntityType(typeof(TEntity)) + ?? (collection.Count > 0 ? null : dbContext.GetService().FindEntityType(collection.First().GetType())) + ?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext); return new UpsertCommandBuilder(dbContext, entityType, collection); }