Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for passing in types that do not match exact entity type #124

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -49,7 +49,7 @@
public UpsertCommandBuilder<TEntity> On(Expression<Func<TEntity, object>> match)
{
if (_matchExpression != null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(On)));

Check warning on line 52 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)

_matchExpression = match ?? throw new ArgumentNullException(nameof(match));
return this;
Expand All @@ -72,12 +72,12 @@
/// <returns>The current instance of the UpsertCommandBuilder</returns>
public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity>> updater)
{
if (updater == null)

Check warning on line 75 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)
throw new ArgumentNullException(nameof(updater));
if (_updateExpression != null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched)));

Check warning on line 78 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)
if (_queryOptions.NoUpdate)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched), nameof(NoUpdate)));

Check warning on line 80 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)

_updateExpression =
Expression.Lambda<Func<TEntity, TEntity, TEntity>>(
Expand All @@ -96,9 +96,9 @@
public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity, TEntity>> updater)
{
if (_updateExpression != null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched)));

Check warning on line 99 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)
if (_queryOptions.NoUpdate)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched), nameof(NoUpdate)));

Check warning on line 101 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)

_updateExpression = updater ?? throw new ArgumentNullException(nameof(updater));
return this;
Expand All @@ -114,7 +114,7 @@
if (condition == null)
throw new ArgumentNullException(nameof(condition));
if (_updateCondition != null)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched)));

Check warning on line 117 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Cache a 'CompositeFormat' for repeated use in this formatting operation (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1863)
if (_queryOptions.NoUpdate)
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.CantCallMethodTwice, nameof(WhenMatched), nameof(NoUpdate)));

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 @@ -23,9 +23,9 @@
public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbContext dbContext, TEntity entity)
where TEntity : class
{
if (dbContext == null)

Check warning on line 26 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)

Check warning on line 26 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs

View workflow job for this annotation

GitHub Actions / Build on Windows

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)
throw new ArgumentNullException(nameof(dbContext));
if (entity == null)

Check warning on line 28 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs

View workflow job for this annotation

GitHub Actions / Build on Ubuntu (full)

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)

Check warning on line 28 in src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs

View workflow job for this annotation

GitHub Actions / Build on Windows

Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1510)
throw new ArgumentNullException(nameof(entity));

return UpsertRange(dbContext, entity);
Expand All @@ -48,6 +48,7 @@
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 @@
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
Loading