EF Database is not seeded
Hello!
I have configured an Entity and the table needs to have an initial row.
After creating the migration and updating the database the row is still not there.
Any ideas what might be missing?
DbContext
Entity
11 Replies
what's in your migration?
The whole database schema, but the seeded data is not there
There is no error or anything, the data is simply not inserted into the Table
if your migration is missing the appropriate SQL to seed the data then you didn't create a migration after you added it
to be clear, i'm talking about the actual c# code that represents the migration
not what your database looks like
I just deleted my whole Migration folder/database to be sure. Ran
Add-Migration
and Update-Database
Stumbled across this GitHub answer by
@roji
: https://github.com/npgsql/efcore.pg/issues/1588#issuecomment-733478755 and there is no difference in my code snippetGitHub
HasData does not work with EntityTypeBuilder · Issue #1588 · npgsql...
.NET 5.0.0 .NET Command-line Tools 5.0.0 I can generate seed data in migrations using DbContext's ModelBuilder.HasData but when I use EntityBuilder.HasData, no seed data is generated.
right but can you check the migration
so we can see if it's even including your seed data in the migration file
if it is a bug you'll need to provide that information anyway
also, did you try the workaround mentioned in that issue?
I haven't, but it looks like that's the only option
Yes this way it works, but now the Db Configuration is not modularized. With multiple seed it will become messy
do you absolutely need to use migrations to seed your data?
i do seed data independently when the application starts based on if the relevant tables are empty or not
Yes, it is critical to have this row in the db by default
that's not what i asked
Sorry. I prefer seeding it with Migration, since it is not for testing purposes
Anyway, thanks for the help! I ended up leaving it in the DbContext