Search code examples
asp.net-coreasp.net-core-identityasp.net-core-2.2

How do I seed Identity roles in ASP.NET Core 2.2 using Script-Migration?


I'm using migrations to create and seed my database. Since my database is running on a separate server from my web application, I'm using Script-Migration to generate a script that I can run on the database server.

According to the official EF Core Docs, we can use HasData to configure seed data for our entities. Is there a way to do this with Identity roles or users?


Solution

  • For seeding data by HasData, it is usually used for your own defined model.

    For seeding data for IdentityUser or IdentityRole, it is recommended to use UserManager<IdentityUser> and RoleManager<IdentityRole>. This is due to that there are many properties may not set correctly by HasData like PasswordHash and ConcurrencyStamp.

    Check this issue How to seed the ASP.NET Identity model, and related seeding questions #736