我正在尝试使用EF Core这样在数据库中创建一个新条目:
RefreshToken newDBRefreshToken = new RefreshToken()
{
Id = 0,
Token = refreshToken,
AccessToken = accessToken,
UserId = userId
};
_dbContext.RefreshToken.Add(newDBRefreshToken);
保存后出现此错误消息:
外键'{UserId:5b82dbb0-c38b-4144-b51d-e092ef6f5e55}'已设置 键值“ {Id:-2147482647}”在“ RefreshToken”上匹配 类型为'IdentityUser'的实体,但是主体实体类型 应该可分配给“ ApplicationUser”。
我有两个实体:
//To make it possible to use AspNetUsers as a reference table
public class ApplicationUser : IdentityUser
{
}
public class RefreshToken : BaseEntity //Has int Id and CreateDate ect...
{
//Reference the IdentityUser
public string UserId { get; set; }
public ApplicationUser User { get; set; }
public string Token { get; set; }
public string AccessToken { get; set; }
public DateTime ValidUntil { get; set; }
}
编辑: 这是我的Startup.cs部分:
services.AddDbContext<ApiContext>(options =>
options.UseSqlServer(connection, builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)
).EnableSensitiveDataLogging(true));
// ===== Add Identity ========
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApiContext>()
.AddErrorDescriber<IdentityErrorDescriberDE>()
.AddDefaultTokenProviders();
我尝试使用ApplicationUser
作为TUser,这给了我这个错误:
InvalidOperationException:无法解析类型的服务 'Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]' 尝试激活“ Web.Controllers.AccountController”时。
这是我的上下文:
public class ApiContext : IdentityDbContext {
...
}