Skip to content

Commit

Permalink
Merge pull request #124 from tsf9810292/feature/support-object-type
Browse files Browse the repository at this point in the history
Support for passing in types that do not match exact entity type
  • Loading branch information
artiomchi authored Nov 20, 2023
2 parents 473d273 + 29f265a commit 5501125
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down
6 changes: 4 additions & 2 deletions src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext
throw new ArgumentNullException(nameof(entities));

var entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity))
?? (entities.Length == 0 ? null : dbContext.GetService<IModel>().FindEntityType(entities.First().GetType()))
?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext);
return new UpsertCommandBuilder<TEntity>(dbContext, entityType, entities);
}
Expand All @@ -67,13 +68,14 @@ public static UpsertCommandBuilder<TEntity> UpsertRange<TEntity>(this DbContext
if (entities == null)
throw new ArgumentNullException(nameof(entities));

var entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity))
?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext);
var collection = entities switch
{
ICollection<TEntity> entityCollection => entityCollection,
_ => entities.ToArray()
};
var entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity))
?? (collection.Count > 0 ? null : dbContext.GetService<IModel>().FindEntityType(collection.First().GetType()))
?? throw new InvalidOperationException(Resources.EntityTypeMustBeMappedInDbContext);
return new UpsertCommandBuilder<TEntity>(dbContext, entityType, collection);
}

Expand Down

0 comments on commit 5501125

Please sign in to comment.