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

3

6

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"
            }
Lui answered 19/12, 2020 at 21:4 Comment(5)
Hmm, HasData requires providing PK values, so it should (is supposed to) work with auto-generated (identity and similar) keys. What EF Core version are you using? And which database provider?Backsight
@IvanStoev Agreed, I thought that was supposed to work as well since the key values are required for HasData. I'm using EF Core 3.1.10, and SqlServer provider.Lui
Strange. Can you try Update-Database -Verbose on clean new database and post the output? Hope you haven't used code similar to context.Database.EnsureCreated(); because it breaks migration system.Backsight
Interestingly enough...it started working. No idea why, I did some minor refactoring in my data project, made some other minor changes, cleaned/rebuilt, etc., and started getting data. But, I really appreciate the time you put into this issue, sorry you had to spend time on it! @IvanStoevLui
Did you create a new migration?? These articles don't state that after you use this method, you have to create a new migration, and in that migration you will then see the migrationBuilder's direct call to InsertData with the data you specified.Limburger
K
5

If the PK column in the database is an Identity Column (created by the database server) then you cannot insert a value. It is generated by the database.

UPDATE

As pointed out in comments below, EF Core will call "SET IDENTITY INSERT ON" when executing HasData().

https://www.learnentityframeworkcore.com/configuration/fluent-api/hasdata-method

Krysta answered 19/12, 2020 at 21:25 Comment(2)
"then you cannot insert a value" Actually you can. For instance, check for identity inserts in SqlServer, the process for other databases might differ. It's the database provider responsibility how to implement that. Note that providing PK value is a requirement (mandatory) for HasData: "The primary key value needs to be specified even if it's usually generated by the database. It will be used to detect data changes between migrations."Backsight
Thanks @IvanStoev, good references. I too figured EF Core would call the set Ident Insert On in the background since the key values are required, and that may be happening, but the strange part is I get no data inserted but no exceptions to indicate anything has gone wrong either.Lui
L
1

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.

Lui answered 21/12, 2020 at 4:35 Comment(1)
HasData is used for generating db migration code. and takes effect by Update-Database.Pygidium
D
1

@Paul Carlton's comment is normally the correct answer to this. After you add the HasData methods you need to run add-migration which in turn generates a migration with all the InsertData() statements to the model.

You then can run update-database as mentioned in @Lei Yangs comment, to populate the data in the database.

Dekow answered 11/4 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.