Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Required property issue when seeding default users for Identity management in entity framework core data migrations

$
0
0

I'm trying to seed 2 default users (admin & standard user) using data migrations and I'm getting an error with the Id property of the IdentityUser entity. This is the code I'm using

internal sealed class IdentityUserConfiguration : IEntityTypeConfiguration<IdentityUser>{    public void Configure(EntityTypeBuilder<IdentityUser> builder)    {        Guid ADMIN_ID = new Guid("0007a13c-6a71-4c93-a7b9-35ea2ba6555e");        Guid ADMIN_ROLE_ID = new Guid("2c5e174e-3b0e-446f-86af-483d56fd7210");        Guid USER_ID = new Guid("8e445865-a24d-4543-a6c6-9443d048cdb9");        Guid USER_ROLE_ID = new Guid("a6c43c31-811b-4c76-82a3-a65fd06078cd");        //a hasher to hash the password before seeding the user to the db        var hasherUser = new PasswordHasher<IdentityUser>();        var hasherAdmin = new PasswordHasher<IdentityUser>();        //Seeding the User to AspNetUsers table        //admin        builder.HasData(            new IdentityUser            {                Id = ADMIN_ID.ToString(), // primary key                ConcurrencyStamp = ADMIN_ROLE_ID.ToString(),                UserName = "test@.com",                NormalizedUserName = "TEST@TEST.COM",                PasswordHash = hasherAdmin.HashPassword(null, "Pa$$w0rd")            }        );        //Seeding the relation between our user and role to AspNetUserRoles table        builder.HasData(            new IdentityUserRole<string>            {                RoleId = ADMIN_ROLE_ID.ToString(),                UserId = ADMIN_ID.ToString()            }        );        //Seeding the User to AspNetUsers table        //standard user        builder.HasData(            new IdentityUser            {                Id = USER_ID.ToString(), // primary key                ConcurrencyStamp = USER_ROLE_ID.ToString(),                UserName = "user@user.com",                NormalizedUserName = "USER@USER.COM",                PasswordHash = hasherUser.HashPassword(null, "Pa$$w0rd")            }        );        //Seeding the relation between our user and role to AspNetUserRoles table        builder.HasData(            new IdentityUserRole<string>            {                RoleId = USER_ROLE_ID.ToString(),                UserId = USER_ID.ToString()            }        );    }}

Ive already created the roles using the same process which worked as expected, however when I try to manually create the migrations, I'm getting this error

"Unable to create a 'DbContext' of type ''. The exception 'The seed entity for entity type 'IdentityUser' cannot be added because no value was provided for the required property 'Id'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728"

A value is clearly provided for the Id property, so why wont this work ? Can anyone see what I'm doing wrong here ?

(I'm using .net 8)


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>