Skip to content

Repeated UpdateData for List<string> property with HasData and migrations #36942

Closed as not planned

Description

@Varorbc

Bug description

When using EF Core's HasData to seed data for an entity that contains a List property, every time I run Add-Migration, even if nothing has changed in the entity or the seed data, EF Core generates UpdateData statements for the List field.

Your code

public class MyEntity
{
    public int Id { get; set; }
    public List<string> Tags { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>().HasData(
        new MyEntity
        {
            Id = 1,
            Tags = new List<string> { "A", "B", "C" }
        }
    );
}

Stack traces


Verbose output


EF Core version

9.0.4

Database provider

Npgsql.EntityFrameworkCore.PostgreSQL

Target framework

.NET 9.0

Operating system

Windows 11

IDE

Visual Studio 2022 17.4

Activity

  1. roji commented on Oct 10, 2025

    @roji
    Member

    I am unable to reproduce this; with the minimal program below, the initial migration contains CreateTable and InsertData operations, and adding another migration after that creates an empty migration. The same happens when the seeding data is added in a separate migration: the first migration contains CreateTable only, the second contains InsertData, and the third is empty.

    As always, we need a fully runnable repro rather than a code snippet.

    Attempted repro
    await using var context = new BlogContext();
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
    
    public class BlogContext : DbContext
    {
        public DbSet<MyEntity> MyEntities { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MyEntity>().HasData(
                    new MyEntity
                    {
                        Id = 1,
                        Tags = new List<string> { "A", "B", "C" }
                    }
                );
        }
    }
    
    public class MyEntity
    {
        public int Id { get; set; }
        public List<string> Tags { get; set; }
    }
  2. Varorbc commented on Oct 11, 2025

    @Varorbc
    ContributorAuthor

    @roji Sorry, I used the wrong database provider to report the issue. Currently, the problem only recurs under npgsql

    npgsql/efcore.pg#3635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions