© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
1 reply
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
Core
,
IdentityCore
IdentityCore
,
EFCore
EFCore
,
CQRSCore
CQRSCore
and
WEB
WEB
)

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

And the folowing
AppDbContext
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());
    }
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'.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Migration issues -EF core.
C#CC# / help
16mo ago
✅ Adding EF Core Migration Error
C#CC# / help
4y ago
EF Core Add-Migration only includes key columns [Answered]
C#CC# / help
4y ago
EF-Core migrations issue
C#CC# / help
2y ago