EF Core Data Seeding (HasData method on modelBuilder) not inserting data for Identity column

Viewed 3965

I'm calling the HasData method (see below) for an entity that has an Id (Identity) column, but no data is inserted into the table the first time the Migration runs.

When the TestObject model was not using an Id (Identity) column for the Primary Key, data was inserted as expected the first time the migration was run.

Is EF Core able to insert data for Identity columns?

modelBuilder.Entity<TestObject>().HasData(
            new TestObject
            {
                Id = 1,
                TestValue = "Test 1"
            }
3 Answers

Functionality began working as expected. Not sure of the cause or the solution, just made changes in the data project, refactored a class or two, cleaned/rebuilt as normal, and after a couple cycles I started getting data in the db upon Update-Database.

After you will add a migration you will see a new generated migration with all your changes

without context

Add-Migration "you_migration_title_here" 

with context

Add-Migration "you_migration_title_here" -Context YourContext
Related