Skip to content

Seeding data via HasData that contains new lines includes undesired tabs when a migration is generated #20662

Closed

Description

@shuhkla

Seeding data via HasData that contains new lines includes undesired tabs when a migration is generated. The string outputted in the migration is not equal to the one in HasData method.

This only appears to happen when the migration produces InsertData call and values being seeded in is formed as a jagged array.

Steps to reproduce

I'm unable to reproduce this reliably, but here's a rough example of what's happening locally for me:

Given the following seed,

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<TestModel>()
        .HasData(
        new TestModel()
        {
          Id = 1,
          Description = @"This is a test.
Notice the linebreak."
        },
        new TestModel()
        {
          Id = 2
          Description = @"This is a second test.
Notice the linebreak."
        },  ... 100 more of these with linebreaks
);
    }

If you generate a migration, the InsertData is scripted as following:

            migrationBuilder.InsertData(
                table: "TestModel",
                columns: new[] { "Id", "Description" },
                values: new object[,]
                {
                    { 1, @"This is a test.
                    Notice the linebreak." },
                    { 2, @"This is a second test.
                    Notice the linebreak." }
                }
            );

It appears the migration tool is attempting to format the insertdata call by inserting indents. But the strings are no longer equal. It's also worth noting that this doesn't happen when the migration generates the InsertData calls separately rather than as a single call.

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10

Activity

  1. added this to the 5.0.0 milestone on Apr 17, 2020
  2. modified the milestones: 5.0.0, on May 11, 2020
  3. modified the milestones: , 5.0.0 on Nov 7, 2020
  4. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions