Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation improvement about data-seeding #4703

Open
andremarcondesteixeira opened this issue Apr 8, 2024 · 0 comments
Open

Documentation improvement about data-seeding #4703

andremarcondesteixeira opened this issue Apr 8, 2024 · 0 comments

Comments

@andremarcondesteixeira
Copy link

Type of issue

Missing information

Description

Article: https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding

The article does mention that Migrations should be used to apply data seeding.

However, one could just interpret this as "you need to call .Migrate()" in your code (like I did).

// Program.cs

// hidden code...

using (var scope = app.Services.CreateScope())
{
    scope.ServiceProvider
            .GetRequiredService<ApplicationDbContext>()
            .Database
            .Migrate();
}

app.Start();

After days of banging my head against the wall, trying to figure out why my initial users and roles were not being created, googling for solutions, searching StackOverflow, asking ChatGPT, reading ASP.NET source code, and with no success after many, many, many, many, maaaaaaaaaaaany hours of stress and headaches, I realized by accident that if I delete my migrations, then OnModelCreated does seed the data.

It looks like Entity Framework only applies data seed from model.Entity<Foo>.HasData(bar) when creating migrations.

So, my suggestion is to make it clear that data is ONLY seeded during the migration creation phase, NOT during runtime.

Please make this clearer.

Page URL

https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/modeling/data-seeding.md

Document Version Independent Id

822e8923-2647-f573-0c74-66af903776d8

Article author

@AndriySvyryd

@ajcvickers ajcvickers added this to the Backlog milestone Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants