Skip to content

Commit

Permalink
Fix SQL Server result mapping for bulk insert
Browse files Browse the repository at this point in the history
Fixes #29502
  • Loading branch information
roji committed Nov 16, 2022
1 parent 3dc1fdf commit bffe458
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ private void ApplyPendingBulkInsertCommands()

if (resultSetMapping != ResultSetMapping.NoResults)
{
ResultSetMappings[^1] = ResultSetMapping.LastInResultSet;
ResultSetMappings[^1] &= ~ResultSetMapping.NotLastInResultSet;
ResultSetMappings[^1] |= ResultSetMapping.LastInResultSet;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ protected override string StoreName
=> "NonSharedModelUpdatesTestBase";

[ConditionalTheory] // Issue #29356
[InlineData(false)]
[InlineData(true)]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Principal_and_dependent_roundtrips_with_cycle_breaking(bool async)
{
var contextFactory = await InitializeAsync<DbContext>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<None Update="SqlAzure\adventureworks.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Update\Issue29502.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public static SqlServerTestStore GetOrCreateInitialized(string name)
public static SqlServerTestStore GetOrCreateWithInitScript(string name, string initScript)
=> new(name, initScript: initScript);

public static SqlServerTestStore GetOrCreateWithScriptPath(string name, string scriptPath, bool? multipleActiveResultSets = null)
=> new(name, scriptPath: scriptPath, multipleActiveResultSets: multipleActiveResultSets);
public static SqlServerTestStore GetOrCreateWithScriptPath(
string name,
string scriptPath,
bool? multipleActiveResultSets = null,
bool shared = true)
=> new(name, scriptPath: scriptPath, multipleActiveResultSets: multipleActiveResultSets, shared: shared);

public static SqlServerTestStore Create(string name, bool useFileName = false)
=> new(name, useFileName, shared: false);
Expand Down
370 changes: 370 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Update/Issue29502.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Data.SqlClient;

namespace Microsoft.EntityFrameworkCore.Update;

public class NonSharedModelUpdatesSqlServerTest : NonSharedModelUpdatesTestBase
Expand Down Expand Up @@ -84,6 +86,52 @@ OUTPUT 1
""");
}

[ConditionalFact] // Issue #29502
public virtual async Task Bulk_insert_result_set_mapping()
{
var contextFactory = await InitializeAsync<DbContext>(
onModelCreating: mb =>
{
mb.Entity<User>().ToTable("Users");
mb.Entity<DailyDigest>().ToTable("DailyDigests");
},
createTestStore: () => SqlServerTestStore.GetOrCreateWithScriptPath(
"Issue29502",
Path.Combine("Update", "Issue29502.sql"),
shared: false));

await ExecuteWithStrategyInTransactionAsync(
contextFactory,
async context =>
{
var digests = await context.Set<User>()
.OrderBy(u => u.TimeCreatedUtc)
.Take(23)
.Select(u => new DailyDigest { User = u })
.ToListAsync();
foreach (var digest in digests)
{
context.Set<DailyDigest>().Add(digest);
}
await context.SaveChangesAsync();
});
}

public class User
{
public string Id { get; set; } = null!;
public DateTime TimeCreatedUtc { get; set; }
public ICollection<DailyDigest> DailyDigests { get; set; } = null!;
}

public class DailyDigest
{
public int Id { get; set; }
public User User { get; set; }
}

private void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.EntityFrameworkCore.Update;

public class NonSharedModelUpdatesSqlServerTest : NonSharedModelUpdatesTestBase
public class NonSharedModelUpdatesSqliteTest : NonSharedModelUpdatesTestBase
{
public override async Task Principal_and_dependent_roundtrips_with_cycle_breaking(bool async)
{
Expand Down

0 comments on commit bffe458

Please sign in to comment.