Skip to content

Commit

Permalink
Fix bug where a non-sproc command comes before a sproc command
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Nov 25, 2022
1 parent 3f82c2b commit d17f75e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ protected override void Consume(RelationalDataReader reader)

for (commandIndex = 0;
commandIndex < ResultSetMappings.Count;
commandIndex++, parameterCounter += command.StoreStoredProcedure!.Parameters.Count)
commandIndex++, parameterCounter += command.ColumnModifications.Count)
{
command = ModificationCommands[commandIndex];

Check.DebugAssert(
command.ColumnModifications.All(c => c.UseParameter),
"This code assumes all column modifications involve a DbParameter (see counting above)");

if (!ResultSetMappings[commandIndex].HasFlag(ResultSetMapping.HasOutputParameters))
{
continue;
Expand Down Expand Up @@ -212,10 +216,14 @@ protected override async Task ConsumeAsync(

for (commandIndex = 0;
commandIndex < ResultSetMappings.Count;
commandIndex++, parameterCounter += command.StoreStoredProcedure!.Parameters.Count)
commandIndex++, parameterCounter += command.ColumnModifications.Count)
{
command = ModificationCommands[commandIndex];

Check.DebugAssert(
command.ColumnModifications.All(c => c.UseParameter),
"This code assumes all column modifications involve a DbParameter (see counting above)");

if (!ResultSetMappings[commandIndex].HasFlag(ResultSetMapping.HasOutputParameters))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,45 @@ protected async Task Tpc(bool async, string createSprocSql)
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public abstract Task Non_sproc_followed_by_sproc_commands_in_the_same_batch(bool async);

protected async Task Non_sproc_followed_by_sproc_commands_in_the_same_batch(bool async, string createSprocSql)
{
var contextFactory = await InitializeAsync<DbContext>(
modelBuilder => modelBuilder.Entity<Entity>()
.InsertUsingStoredProcedure(
nameof(Entity) + "_Insert",
spb => spb
.HasParameter(w => w.Name)
.HasParameter(w => w.Id, pb => pb.IsOutput())),
seed: ctx => CreateStoredProcedures(ctx, createSprocSql));

await using var context = contextFactory.CreateContext();

// Prepare by adding an entity
var entity1 = new Entity { Name = "Entity1" };
context.Set<Entity>().Add(entity1);

using (TestSqlLoggerFactory.SuspendRecordingEvents())
{
await SaveChanges(context, async);
}

// Now add a second entity and delete the first one. Deletion gets ordered first, and doesn't use a sproc, and then the insertion
// does.
var entity2 = new Entity { Name = "Entity2" };
context.Set<Entity>().Add(entity2);
context.Set<Entity>().Remove(entity1);
await SaveChanges(context, async);

using (TestSqlLoggerFactory.SuspendRecordingEvents())
{
Assert.Equal("Entity2", context.Set<Entity>().Single(b => b.Id == entity2.Id).Name);
}
}

/// <summary>
/// A method to be implement by the provider, to set up a store-generated concurrency token shadow property with the given name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,32 @@ AS BEGIN
""");
}

public override async Task Non_sproc_followed_by_sproc_commands_in_the_same_batch(bool async)
{
await base.Non_sproc_followed_by_sproc_commands_in_the_same_batch(
async,
"""
CREATE PROCEDURE Entity_Insert(@Name varchar(max), @Id int OUT)
AS BEGIN
INSERT INTO [Entity] ([Name]) VALUES (@Name);
SET @Id = SCOPE_IDENTITY();
END
""");

AssertSql(
"""
@p0='1'
@p1='Entity2' (Size = 4000)
@p2=NULL (Nullable = false) (Direction = Output) (DbType = Int32)
SET NOCOUNT ON;
DELETE FROM [Entity]
OUTPUT 1
WHERE [Id] = @p0;
EXEC [Entity_Insert] @p1, @p2 OUTPUT;
""");
}

protected override void ConfigureStoreGeneratedConcurrencyToken(EntityTypeBuilder entityTypeBuilder, string propertyName)
=> entityTypeBuilder.Property<byte[]>(propertyName).IsRowVersion();

Expand Down

0 comments on commit d17f75e

Please sign in to comment.