I'm trying customize EntityFrameworkCore Identity with AspNetUsers, AspNetRoles and AspNetUserRoles only... Is it possible?
My code:
ApplicationUser.cs
public class ApplicationUser : IdentityUser<int>
{
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
//base.OnModelCreating(builder);
builder.Entity<IdentityUser<int>>().ForSqlServerToTable("AspNetUsers")
//.Ignore(x => x.Claims)
//.Ignore(x => x.Logins)
.HasKey(x => x.Id);
builder.Entity<IdentityRole<int>>().ForSqlServerToTable("AspNetRoles")
//.Ignore(x => x.Claims)
.HasKey(x => x.Id);
builder.Entity<IdentityUserRole<int>>().ForSqlServerToTable("AspNetUserRoles")
.HasKey(x => new { x.UserId, x.RoleId });
builder.Ignore<IdentityUserLogin<int>>();
builder.Ignore<IdentityUserToken<int>>();
builder.Ignore<IdentityUserClaim<int>>();
builder.Ignore<IdentityRoleClaim<int>>();
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
...
}
AccountController.cs
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
...
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
return View(model);
}
CreateAsync = success, but SignInAsync return InvalidOperationException: Cannot create a DbSet for 'IdentityUserClaim`1' because this type is not included in the model for the context.
My database contains only tables AspNetRoles, AspNetUsers and AspNetUserRoles with default columns(Id's type int).
Thanks.
IdentityUserClaim
etc? – DavidG