nbarbettini / little-aspnetcore-book

The Little ASP.NET Core Book, a friendly introduction to web programming and ASP.NET Core 2.0

Home Page:http://littleasp.net/book

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Seeding data in asp.net core 2.0 should be in Main method

sadqiang opened this issue · comments

IMHO, data seeding in Configure is no longer recommended for Asp.net Core 2.0. Instead it should be done in Program.Main.

Therefore, creating an administrator account

EnsureRolesAsync(roleManager).Wait();
EnsureTestAdminAsync(userManager).Wait();

should be placed in Program.Main.

As a reference: navigate to How to correctly seed a DbContext using ASP.NET Core 2.0?

Cool, thanks for linking to the official repo. I didn't realize the best practice here had changed. 👍

I am having difficulties with using migrations on a database where I need to have it seeded with an admin user, can you elaborate on how this should be done with migrations without causing error when running migration-update?

In the Main method, the 'dotnet ef database update' fails because it needs to have BuildwebHost(args) as exactly that, not var host = BuildWebHost etc etc. But when adding a admin in the configure method in startup as earlier, it fails because "There is already an object named "AspNetRoles" in the database.

Any tips appreciated!
Morten

EDIT:
I found a solution, this worked for me!
https://stackoverflow.com/questions/45148389/how-to-seed-in-entity-framework-core-2