Hi,@Stefan Gunel,I tried as you mentioned and reproduced the error
I think you've messed up the dbcontext for Db-first & Code-first
And the following tables are necessary for your ApplicationUser

For code first solution,you could try with the dbcontext as below:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public virtual DbSet<SomeEntity>SomeEntity { get; set; }
// other DbSets here......
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
public class ApplicationUser : IdentityUser
{
.......
public SomeEntity SomeEntity { get; set; } = default!;
}
other DbSets such as DbSet<AspNetUsers> AspNetUsers
has been defined in parent class so you don't have to define it again,also the entity has been configured in parent class's OnModelCreating
method ,if you want to keep the configration,just call base.OnModelCreating(builder) in child class(If you forget to call it it child class,the primary key for AspNetUserLogin won't be configured and you would get the error)
here's the source codes related:
builder.Entity<TUserLogin>(b =>
{
b.HasKey(l => new { l.LoginProvider, l.ProviderKey });
if (maxKeyLength > 0)
{
b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
}
b.ToTable("AspNetUserLogins");
});
And the Scaffolded dbcontext on my side:
public partial class Identity0523Context : DbContext
{
public Identity0523Context()
{
}
public Identity0523Context(DbContextOptions<Identity0523Context> options)
: base(options)
{
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetRoleClaim> AspNetRoleClaims { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUserToken> AspNetUserTokens { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("connectstr");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AspNetRole>(entity =>
{
entity.HasIndex(e => e.NormalizedName, "RoleNameIndex")
.IsUnique()
.HasFilter("([NormalizedName] IS NOT NULL)");
entity.Property(e => e.Name).HasMaxLength(256);
entity.Property(e => e.NormalizedName).HasMaxLength(256);
});
modelBuilder.Entity<AspNetRoleClaim>(entity =>
{
entity.HasIndex(e => e.RoleId, "IX_AspNetRoleClaims_RoleId");
entity.HasOne(d => d.Role).WithMany(p => p.AspNetRoleClaims).HasForeignKey(d => d.RoleId);
});
modelBuilder.Entity<AspNetUser>(entity =>
{
entity.HasIndex(e => e.NormalizedEmail, "EmailIndex");
entity.HasIndex(e => e.NormalizedUserName, "UserNameIndex")
.IsUnique()
.HasFilter("([NormalizedUserName] IS NOT NULL)");
entity.Property(e => e.Email).HasMaxLength(256);
entity.Property(e => e.NormalizedEmail).HasMaxLength(256);
entity.Property(e => e.NormalizedUserName).HasMaxLength(256);
entity.Property(e => e.UserName).HasMaxLength(256);
entity.HasMany(d => d.Roles).WithMany(p => p.Users)
.UsingEntity<Dictionary<string, object>>(
"AspNetUserRole",
r => r.HasOne<AspNetRole>().WithMany().HasForeignKey("RoleId"),
l => l.HasOne<AspNetUser>().WithMany().HasForeignKey("UserId"),
j =>
{
j.HasKey("UserId", "RoleId");
j.ToTable("AspNetUserRoles");
j.HasIndex(new[] { "RoleId" }, "IX_AspNetUserRoles_RoleId");
});
});
modelBuilder.Entity<AspNetUserClaim>(entity =>
{
entity.HasIndex(e => e.UserId, "IX_AspNetUserClaims_UserId");
entity.HasOne(d => d.User).WithMany(p => p.AspNetUserClaims).HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AspNetUserLogin>(entity =>
{
entity.HasKey(e => new { e.LoginProvider, e.ProviderKey });
entity.HasIndex(e => e.UserId, "IX_AspNetUserLogins_UserId");
entity.Property(e => e.LoginProvider).HasMaxLength(128);
entity.Property(e => e.ProviderKey).HasMaxLength(128);
entity.HasOne(d => d.User).WithMany(p => p.AspNetUserLogins).HasForeignKey(d => d.UserId);
});
modelBuilder.Entity<AspNetUserToken>(entity =>
{
entity.HasKey(e => new { e.UserId, e.LoginProvider, e.Name });
entity.Property(e => e.LoginProvider).HasMaxLength(128);
entity.Property(e => e.Name).HasMaxLength(128);
entity.HasOne(d => d.User).WithMany(p => p.AspNetUserTokens).HasForeignKey(d => d.UserId);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
You missed the required Entities to describe ApplicationUser/IdentityUser and when you try this line
var result = await _userManager.CreateAsync(user, Input.Password);
You would got error,I recommand you just try code-first approach with the dbcontext I've shown and follow this document to customize Identity
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
RuikaiFeng