C#C
C#3y ago
M B V R K

❔ EF Core Add Migration issue

I'm working on an ASP.Net Core 7 API project, this project is in a solution that contains 5 projects ( Core, IdentityCore, EFCore, CQRSCore and WEB)

I try to integrate the Ientity in my solution by using the folowing AppUser entity:
public class AppUser : IdentityUser<int>
{
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
}

And the folowing AppDbContext
public class AppDbContext : IdentityDbContext<AppUser, IdentityRole<int>, int>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }
    private AppRole[] CreateRoles()
    {
        var role1 = new AppRole
        {
            Id = 1,
            Name = "Admin",
            NormalizedName = "ADMIN"
        };

        var role2 = new AppRole
        {
            Id = 2,
            Name = "User",
            NormalizedName = "USER"
        };

        return new AppRole[] { role1, role2 };
    }
    
   
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);



        base.OnModelCreating(builder);

        builder.Entity<IdentityRole>().HasData(CreateRoles());
        builder.Entity<AppUser>().HasData(CreateAppUsers());
        builder.Entity<AppRole>().HasData(CreateAppUserRoles());
    }


Why I get this error when try to add a new Migration
The seed entity for entity type 'AppRole' cannot be added because no value was provided for the required property 'Id'.
Was this page helpful?