Skip to content

Commit

Permalink
* Preparing for v2 release, marking older methods as obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Sep 6, 2018
1 parent 72d0ced commit 779f610
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -15,8 +15,8 @@ Also supports injecting sql command generators to add support for other provider
<RepositoryUrl>https://github.com/artiomchi/FlexLabs.Upsert</RepositoryUrl>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageTags>Entity Framework Core entity-framework-core EF EntityFramework EntityFrameworkCore EFCore Upsert</PackageTags>
<VersionPrefix>1.0.4</VersionPrefix>
<PackageReleaseNotes>! Patched the crash when using `.On()` with a single column</PackageReleaseNotes>
<VersionPrefix>1.1.0</VersionPrefix>
<PackageReleaseNotes>* Preparing for v2 release, marking older methods as obsolete</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TEntity> Upsert<TEntity>(this DbContext dbContext, TEntity entity)
where TEntity : class
{
var entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity));
return new UpsertCommandBuilder<TEntity>(dbContext, entityType, entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public UpsertCommandBuilder<TEntity> On(Expression<Func<TEntity, object>> match)
return this;
}

[Obsolete("Replaced with `WhenMatched`")]
public UpsertCommandBuilder<TEntity> UpdateColumns(Expression<Func<TEntity, TEntity>> updater)
=> WhenMatched(updater);

public UpsertCommandBuilder<TEntity> WhenMatched(Expression<Func<TEntity, TEntity>> updater)
{
if (_updateValues != null)
throw new InvalidOperationException($"Can't call {nameof(UpdateColumns)} twice!");
Expand Down
12 changes: 10 additions & 2 deletions src/FlexLabs.EntityFrameworkCore.Upsert/UpsertExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -12,5 +13,12 @@ public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbContext dbCon
var entityType = dbContext.GetService<IModel>().FindEntityType(typeof(TEntity));
return new UpsertCommandBuilder<TEntity>(dbContext, entityType, entity);
}

public static UpsertCommandBuilder<TEntity> Upsert<TEntity>(this DbSet<TEntity> dbSet, TEntity entity)
where TEntity : class
{
var dbContext = dbSet.GetService<ICurrentDbContext>().Context;
return Upsert(dbContext, entity);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<TGenerator>(this IServiceCollection services)
where TGenerator : class, IUpsertSqlGenerator
{
Expand Down

0 comments on commit 779f610

Please sign in to comment.