Search code examples
c#asp.net-coreasp.net-identity

IdentityDbContext throws error "The entity type 'User' requires a primary key to be defined."


I want to create Identityuser for my application. I create dbcontext using Identitydbcontext.

Here is my code - ShopContext.cs file:

public class ShopContext: IdentityDbContext<UserEntity, UserRoleEntity, Guid>
{
    public ShopContext(DbContextOptions options)
         : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<UserEntity> Users { get; set; }
}

Now I make userentity and userrole class using identity:

public class UserRoleEntity : IdentityRole<Guid>
{
    public UserRoleEntity()
        : base()
    {
    }

    public UserRoleEntity(string roleName)
        : base(roleName)
    {
    }
}

Userentity class:

 public class UserEntity : IdentityUser<Guid>
{

    public Guid Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTimeOffset CreatedAt { get; set; }
}

Now I try to feed data into my product DbSet as at the moment I don't have actual dataset - like this:

public class SeedData
{

    public static async Task InitializeAsync(IServiceProvider services)
    {

        await AddTestData(
           services.GetRequiredService<ShopContext>(),
           services.GetRequiredService<UserManager<UserEntity>>());

        await AddTestUsers(
           services.GetRequiredService<RoleManager<UserRoleEntity>>(),
           services.GetRequiredService<UserManager<UserEntity>>());

    }

    private static async Task AddTestData(ShopContext shopContext, UserManager<UserEntity> userManager)
    {
        try
        {
            shopContext.Products.Add(new Product
            {
                Id = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
                CategoryId = 1,
                Name = "Grunge Skater Jeans",
                Sku = "AWMGSJ",
                Price = 68,
                IsAvailable = true
            });

            var adminUser = userManager.Users

                .SingleOrDefault(u => u.Email == "[email protected]");

            await shopContext.SaveChangesAsync();

        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    private static async Task AddTestUsers(RoleManager<UserRoleEntity> roleManager, UserManager<UserEntity> userManager)
    {
        var dataExists = roleManager.Roles.Any() || userManager.Users.Any();
        if (dataExists)
        {
            return;
        }

        // Add a test role
        await roleManager.CreateAsync(new UserRoleEntity("Admin"));

        // Add a test user
        var user = new UserEntity
        {
            Id = Guid.Parse("ee2b83be-91db-4de5-8122-35a9e9195976"),
            Email = "[email protected]",
            UserName = "[email protected]",
            FirstName = "Admin",
            LastName = "Tester",
            CreatedAt = DateTimeOffset.UtcNow
        };

        await userManager.CreateAsync(user, "Supersecret123!!");

        // Put the user in the admin role
        await userManager.AddToRoleAsync(user, "Admin");
        await userManager.UpdateAsync(user);
    }
}

Now here I got error when I try to add in products dbset.check the image:

Error image

Here is my product.cs class:

public class Product
{
   
    public Guid Id { get; set; }
    public string Sku { get; set; }
    [Required]
    public string Name { get; set; }
    [MaxLength(255)]
    public string Description { get; set; }
    public decimal Price { get; set; }
    public bool IsAvailable { get; set; }

    public int CategoryId { get; set; }
    [JsonIgnore]
    public virtual Category Category { get; set; }
}

The error pointed me I am missing primary key in userentity but I already added Key annotation for firstname.

My service configuration in startup.cs class:-

 public void ConfigureServices(IServiceCollection services)
    {

        services.AddScoped<IProductService, ProductService>();
        services.AddScoped<IUserService, UserService>();

        services.AddDbContext<ShopContext>(
            options =>
            {
                options.UseInMemoryDatabase("landondb");
                options.UseOpenIddict<Guid>();

            });
                              

        // Add ASP.NET Core Identity
        AddIdentityCoreServices(services);           


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

private void AddIdentityCoreServices(IServiceCollection services)
    {
        var builder = services.AddIdentityCore<UserEntity>();
        builder = new IdentityBuilder(builder.UserType, typeof(UserRoleEntity), builder.Services);
        builder.AddRoles<UserRoleEntity>().AddEntityFrameworkStores<ShopContext>()
            .AddDefaultTokenProviders()
            .AddSignInManager<SignInManager<UserEntity>>();
    }

Any Idea why I am getting this error:-

InvalidOperationException: The entity type 'User' requires a primary key to be defined.

Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model)
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model)
Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)


Solution

  • first userentity model has id? and in your code for userentity you don't assigned id value, @sangita-paul's code is :

    public class UserEntity : IdentityUser<Guid>
    {
    
        [Key]
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTimeOffset CreatedAt { get; set; }
    }
    

    has not id, so I think userentity need id for primary key and assigned value that