So how does one seed the admin user along with his identity data including roles and claims in ASP.NET Core 2.1.0 that is using MSSQL Server as the data store based on EF Core 2.1.0. I have used the HasData() function to load non-identity tables with no issue but do not understand how the user identity and related tables should be loaded. Can some one point me to a specific example loading these tables?
Another question is, after calling HasData() on any tables the next time this called and a table has data will it not add data again?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@Orgbrat Seed data is migrated with the schema. If you don't add more data then no rows will be inserted.
As I stated above I have seeded data in the Context for tables not related to Identity already successfully.
But my current question is how does one seed the website admin user along with his identity data including roles and claims in ASP.NET Core 2.1.0. There has to be a way to seed an initial user usually a web site default administrator for the website with all his correct roles and claims. In previous versions we wrote code that used the User Manager and Role Manager to programmatically perform adding a default user. That is the process I am trying to determine how to do at the time of scheme creation in the OnModelCreating(ModelBuilder modelBuilder) function. If you take notice of the Data Seeding document it looks like I am not the only person having issues understand how to do this process.
Orgbrat
You can seed data for identity similarly to before: http://www.binaryintellect.net/articles/5e180dfa-4438-45d8-ac78-c7cc11735791.aspx
Duplicate of #736
While I would agree with @ajcvickers that looking at the titles this seems to be a duplicate of #736, I don't think it is! On #736 they seem to be using the term "identity" to discuss primary key columns when inserting seed data using .HasData(...), where as this issue is actually focused specifically on seeding data for ASP.NET Core's identity system. Please consider reopening this issue.
@HaoK the link you provided isn't very helpful because it is for 2.0 not 2.1 which is what this issue is about. In 2.1 they have changed how you build and run the host so you can no longer create the scope in the same way. In fact I cannot seem to find a way to create the scope needed to create a service provider to get instances of the UserManager and SignInManager from identity.
I am also stuck with same issue. I am trying to utilize the recent Data Seeding additions to Entity Framework Core 2.1: https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding. But I cannot find a way to obtain a UserManager for creating the initial Users.
Is there a recommended way to seed data for ASP.NET Core Identity?
I never actually worked out what the "proper" way to seed the identity tables is sadly. But you can use the normal seeding methods to insert directly into the tables quiet easily. There are a couple of "stamp" columns (whose exact name I can't remember right now) that you can just put Guids in, and the "NORMALISED" columns just need to be all caps versions of username/email.
Yes, that's basically what I did:
It's working. But honestly, I don't know if it is actually the right place to seed the initial data.
I beginning to think there isn't currently a specific way to seed the identity tables, and they are simple enough that doing it the standard way works fine.
I would note that I. Your code you aren't setting the stamp columns. I am pretty sure they are required to have an initial value if you intend on using the user/sign-in managers down the line
Most helpful comment
While I would agree with @ajcvickers that looking at the titles this seems to be a duplicate of #736, I don't think it is! On #736 they seem to be using the term "identity" to discuss primary key columns when inserting seed data using
.HasData(...), where as this issue is actually focused specifically on seeding data for ASP.NET Core's identity system. Please consider reopening this issue.@HaoK the link you provided isn't very helpful because it is for 2.0 not 2.1 which is what this issue is about. In 2.1 they have changed how you build and run the host so you can no longer create the scope in the same way. In fact I cannot seem to find a way to create the scope needed to create a service provider to get instances of the
UserManagerandSignInManagerfrom identity.