diff --git a/README.md b/README.md index 6c02b4c..cda13c7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Also supports injecting sql command generators to add support for other provider In it's simplest form, it can be used as follows: ```csharp -DataContext.Upsert(new Country +DataContext.Countries.Upsert(new Country { Name = "Australia", ISO = "AU", @@ -29,14 +29,14 @@ If the entry already exists, the command will update the remaining columns to ma In some cases, you don't want ALL the entities to be changed. An example field that you wouldn't want updated is the `Created` field. You can use a third parameter to select which columns and values to set in case the entity already exists: ```csharp -DataContext.Upsert(new Country +DataContext.Countries.Upsert(new Country { Name = "Australia", ISO = "AU", Created = DateTime.UtcNow, }) .On(c => c.ISO) - .UpdateColumns(c => new Country + .WhenMatched(c => new Country { Name = "Australia" Updated = DateTime.UtcNow, @@ -47,14 +47,14 @@ DataContext.Upsert(new Country Finally, sometimes you might want to update a column based on the current value in the table. For example, if you want to increment a column. You can use the following syntax (basic support for incrementing and decrementing values is currently implemented): You can also see how to implement the multi column record matching: ```csharp -DataContext.Upsert(new DailyVisits +DataContext.DailyVisits.Upsert(new DailyVisit { UserID = userID, Date = DateTime.UtcNow.Date, Visits = 1, }) .On(v => new { v.UserID, v.Date }) - .UpdateColumns(v => new DailyVisits + .WhenMatched(v => new DailyVisit { Visits = v.Visits + 1, }) diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/FlexLabs.EntityFrameworkCore.Upsert.csproj b/src/FlexLabs.EntityFrameworkCore.Upsert/FlexLabs.EntityFrameworkCore.Upsert.csproj index cfb7892..995d439 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/FlexLabs.EntityFrameworkCore.Upsert.csproj +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/FlexLabs.EntityFrameworkCore.Upsert.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -15,8 +15,8 @@ Also supports injecting sql command generators to add support for other provider https://github.com/artiomchi/FlexLabs.Upsert https://opensource.org/licenses/MIT Entity Framework Core entity-framework-core EF EntityFramework EntityFrameworkCore EFCore Upsert - 1.0.4 - ! Patched the crash when using `.On()` with a single column + 1.1.0 + * Preparing for v2 release, marking older methods as obsolete diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/ObsoleteUpsertExtensions.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/ObsoleteUpsertExtensions.cs new file mode 100644 index 0000000..13eb06f --- /dev/null +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/ObsoleteUpsertExtensions.cs @@ -0,0 +1,18 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; + +namespace FlexLabs.EntityFrameworkCore.Upsert +{ + public static class ObsoleteUpsertExtensions + { + [Obsolete("This extension method was moved to the Microsoft.EntityFrameworkCore namespace")] + public static UpsertCommandBuilder Upsert(this DbContext dbContext, TEntity entity) + where TEntity : class + { + var entityType = dbContext.GetService().FindEntityType(typeof(TEntity)); + return new UpsertCommandBuilder(dbContext, entityType, entity); + } + } +} diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs index c89c92e..f2a3b0c 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertCommandBuilder.cs @@ -74,7 +74,11 @@ public UpsertCommandBuilder On(Expression> match) return this; } + [Obsolete("Replaced with `WhenMatched`")] public UpsertCommandBuilder UpdateColumns(Expression> updater) + => WhenMatched(updater); + + public UpsertCommandBuilder WhenMatched(Expression> updater) { if (_updateValues != null) throw new InvalidOperationException($"Can't call {nameof(UpdateColumns)} twice!"); diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs index 54fae4d..43b8c48 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs @@ -1,8 +1,9 @@ -using Microsoft.EntityFrameworkCore; +using FlexLabs.EntityFrameworkCore.Upsert; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Metadata; -namespace FlexLabs.EntityFrameworkCore.Upsert +namespace Microsoft.EntityFrameworkCore { public static class UpsertExtensions { @@ -12,5 +13,12 @@ public static UpsertCommandBuilder Upsert(this DbContext dbCon var entityType = dbContext.GetService().FindEntityType(typeof(TEntity)); return new UpsertCommandBuilder(dbContext, entityType, entity); } + + public static UpsertCommandBuilder Upsert(this DbSet dbSet, TEntity entity) + where TEntity : class + { + var dbContext = dbSet.GetService().Context; + return Upsert(dbContext, entity); + } } } diff --git a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertServiceExtensions.cs b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertServiceExtensions.cs index dfcf3bb..58ba1aa 100644 --- a/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertServiceExtensions.cs +++ b/src/FlexLabs.EntityFrameworkCore.Upsert/UpsertServiceExtensions.cs @@ -1,9 +1,11 @@ -using FlexLabs.EntityFrameworkCore.Upsert.Generators; +using System; +using FlexLabs.EntityFrameworkCore.Upsert.Generators; namespace Microsoft.Extensions.DependencyInjection { public static class UpsertServiceExtensions { + [Obsolete("Marking as obsolete to replace with a new, working one in v2")] public static IServiceCollection AddUpsertCommandGenerator(this IServiceCollection services) where TGenerator : class, IUpsertSqlGenerator {