Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding no longer takes max batch size into account #28876

Closed
kccsf opened this issue Aug 24, 2022 · 1 comment · Fixed by #28955
Closed

Seeding no longer takes max batch size into account #28876

kccsf opened this issue Aug 24, 2022 · 1 comment · Fixed by #28955
Assignees
Labels
area-migrations-seeding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@kccsf
Copy link

kccsf commented Aug 24, 2022

Adding >1000 rows of seed data to a single entity generates a migration that fails. Should the resulting migration be splitting inserts into batches of 1000 records?:


using var test = new BloggingContext();
await test.Database.MigrateAsync();

class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; } = null!;

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlServer(@"Server=localhost,53133;Database=EFTest;Trusted_Connection=True;Encrypt=false;MultipleActiveResultSets=true;");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var blogs = Enumerable.Range(1, 1001)
            .Select(x => new Blog(x));

        modelBuilder.Entity<Blog>()
            .HasData(blogs);
    }
}

record Blog(int BlogId);
Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlCommand.InternalEndExecuteNonQuery(IAsyncResult asyncResult, Boolean isInternal, String endMethod)
   at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable`1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable`1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable`1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQueryAsync(IEnumerable`1 migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in C:\Users\xxxxx\source\repos\EFTest\Program.cs:line 4
   at Program.<Main>(String[] args)
ClientConnectionId:
Error Number:10738,State:1,Class:15

EF Core version: 7.0.0-preview.7.22376.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows
IDE: None (command line)

@roji
Copy link
Member

roji commented Aug 25, 2022

Confirmed, we no longer apply the SQL Server 42-commands-per-batch limit in seeding.

In 6.0, MigrationsModelDiffer.GetDataOperations has the following:

var model = updateAdapter.Model.GetRelationalModel();
var commandBatches = new CommandBatchPreparer(CommandBatchPreparerDependencies)
    .BatchCommands(entries, updateAdapter);

In 7.0, we just have this:

var commandSets = new CommandBatchPreparer(CommandBatchPreparerDependencies)
    .TopologicalSort(commands);

@AndriySvyryd this change was part of #27903 (here's the code).

@ajcvickers ajcvickers added this to the 7.0.0 milestone Aug 31, 2022
@roji roji changed the title 7.0.0-preview.7.22376.2 / SQL Server / Migration Issue: The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values Seeding no longer takes max batch size into account Sep 1, 2022
@roji roji added the regression label Sep 1, 2022
roji added a commit to roji/efcore that referenced this issue Sep 1, 2022
@roji roji added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. area-migrations-seeding and removed area-save-changes labels Sep 1, 2022
roji added a commit to roji/efcore that referenced this issue Sep 1, 2022
roji added a commit to roji/efcore that referenced this issue Sep 6, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 9, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations-seeding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants