Skip to content

Commit

Permalink
! If no columns are to be updated, build the correct sql statement
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Dec 18, 2018
1 parent c066812 commit b517f20
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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>2.0.2</VersionPrefix>
<VersionPrefix>2.0.3</VersionPrefix>
<PackageReleaseNotes>
+ Adding support for EF 2.1 Type Conversions
+ Adding support for static property/field accessors (e.g. DateTime.Now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public interface IUpsertCommandRunner
/// <param name="matchExpression">Expression that represents which properties will be used as a match clause for the upsert command</param>
/// <param name="updateExpression">Expression that represents which properties will be updated, and what values will be set</param>
/// <param name="noUpdate">Specifies that if a match is found, no action will be taken on the entity</param>
/// <param name="useExpressionCompiler">If true, will fallback to the (slower) expression compiler for unhandled update expressions</param>
void Run<TEntity>(DbContext dbContext, IEntityType entityType, ICollection<TEntity> entities, Expression<Func<TEntity, object>> matchExpression,
Expression<Func<TEntity, TEntity, TEntity>> updateExpression, bool noUpdate, bool useExpressionCompiler) where TEntity : class;

Expand All @@ -43,6 +44,7 @@ void Run<TEntity>(DbContext dbContext, IEntityType entityType, ICollection<TEnti
/// <param name="matchExpression">Expression that represents which properties will be used as a match clause for the upsert command</param>
/// <param name="updateExpression">Expression that represents which properties will be updated, and what values will be set</param>
/// <param name="noUpdate">Specifies that if a match is found, no action will be taken on the entity</param>
/// <param name="useExpressionCompiler">If true, will fallback to the (slower) expression compiler for unhandled update expressions</param>
/// <param name="cancellationToken">The CancellationToken to observe while waiting for the task to complete.</param>
/// <returns>The task that represents the asynchronous upsert operation</returns>
Task RunAsync<TEntity>(DbContext dbContext, IEntityType entityType, ICollection<TEntity> entities, Expression<Func<TEntity, object>> matchExpression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ protected virtual string GetSchema(IEntityType entityType)
foreach (var arg in arguments)
arg.ArgumentIndex = i++;

var columnUpdateExpressions = updateExpressions?.Select(x => (x.Property.Relational().ColumnName, x.Value)).ToArray();
var columnUpdateExpressions = updateExpressions?.Count > 0
? updateExpressions.Select(x => (x.Property.Relational().ColumnName, x.Value)).ToArray()
: null;
var sqlCommand = GenerateCommand(GetTableName(entityType), newEntities, joinColumnNames, columnUpdateExpressions);
return (sqlCommand, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ public class StringKey
public string ID { get; set; }
public string Name { get; set; }
}

public class KeyOnly
{
public int ID1 { get; set; }
public int ID2 { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<DashTable>().HasIndex(t => t.DataSet).IsUnique();
modelBuilder.Entity<PageVisit>().HasIndex(pv => new { pv.UserID, pv.Date }).IsUnique();
modelBuilder.Entity<SchemaTable>().HasIndex(t => t.Name).IsUnique();
modelBuilder.Entity<KeyOnly>().HasKey(t => new { t.ID1, t.ID2 });

var dbProvider = this.GetService<IDatabaseProvider>();
if (dbProvider.Name == "Npgsql.EntityFrameworkCore.PostgreSQL")
Expand All @@ -38,6 +39,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<GuidKey> GuidKeys { get; set; }
public DbSet<StringKeyAutoGen> StringKeysAutoGen { get; set; }
public DbSet<StringKey> StringKeys { get; set; }
public DbSet<KeyOnly> KeyOnlies { get; set; }

public enum DbDriver
{
Expand Down
26 changes: 26 additions & 0 deletions test/FlexLabs.EntityFrameworkCore.Upsert.Tests/EF/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private void ResetDb(TestDbContext.DbDriver driver)
dbContext.GuidKeys.RemoveRange(dbContext.GuidKeys);
dbContext.StringKeysAutoGen.RemoveRange(dbContext.StringKeysAutoGen);
dbContext.StringKeys.RemoveRange(dbContext.StringKeys);
dbContext.KeyOnlies.RemoveRange(dbContext.KeyOnlies);

dbContext.Countries.Add(_dbCountry);
dbContext.PageVisits.Add(_dbVisitOld);
Expand Down Expand Up @@ -1358,5 +1359,30 @@ public void Upsert_StringKey(TestDbContext.DbDriver driver)
j => Assert.Equal(newItem.ID, j.ID));
}
}

[Theory]
[MemberData(nameof(GetDatabaseEngines))]
public void Upsert_KeyOnly(TestDbContext.DbDriver driver)
{
ResetDb(driver);
using (var dbContext = new TestDbContext(_dataContexts[driver]))
{
var newItem = new KeyOnly
{
ID1 = 123,
ID2 = 456,
};

dbContext.KeyOnlies.Upsert(newItem)
.Run();

Assert.Collection(dbContext.KeyOnlies.OrderBy(j => j.ID1),
j =>
{
Assert.Equal(newItem.ID1, j.ID1);
Assert.Equal(newItem.ID2, j.ID2);
});
}
}
}
}

0 comments on commit b517f20

Please sign in to comment.