我想与AspNetUsers,AspNetRoles和AspNetUserRoles自定义EntityFrameworkCore身份...这可能吗?自定义标识EntityFrameworkCore
我的代码:
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 =成功,但SignInAsync回报出现InvalidOperationException:无法创建 'IdentityUserClaim`1' 一个DbSet因为这种类型没有在模型包括为上下文。
我的数据库仅包含带有默认列的AspNetRoles,AspNetUsers和AspNetUserRoles(Id的类型为int)。
感谢。
为什么额外的4行忽略'IdentityUserClaim'等? – DavidG
忽略IdentityUserLogin!= InvalidOperationException:实体类型'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin'需要定义一个密钥。 –
没有额外的行返回 - > IdentityUserLogin要求定义一个关键。 IdentityUserToken 需要定义一个密钥。 SqlException:无效的对象名称'UserClaims'。 SqlException:无效的对象名称'RolerClaims'。 –