- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
情况
我有一个我更新的工作程序需要更新身份/登录方法。所以我从 Identity 1 更新到 version2 并且我一直在修复错误,但我现在很困惑。
这是我的注册码
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//try
//{
var user = new ApplicationUser() { UserName = model.Email,Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
问题
发生的问题是我在 var result = await UserManager.CreateAsync(user, model.Password);
行上得到了一个无效的列名'UserId'
.
我不完全确定为什么会发生这种情况(或者这个 UserId
来自哪里,可能来自旧身份?)因为当我在行上放置断点时,SQL 将查询显示为
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Email] AS [Email],
[Extent1].[EmailConfirmed] AS [EmailConfirmed],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
[Extent1].[PhoneNumber] AS [PhoneNumber],
[Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Extent1].[LockoutEnabled] AS [LockoutEnabled],
[Extent1].[AccessFailedCount] AS [AccessFailedCount],
[Extent1].[UserName] AS [UserName]
FROM [dbo].[AspNetUsers] AS [Extent1]
我通过将 UserId
添加到 AspNetUsers 表来调和程序,然后发生了两件事。
Id
重命名为 UserId
并制作普通字段 ID 时。然后我得到的错误是
InnerException = {"Cannot insert the value NULL into column 'UserId', table 'dbo.AspNetUsers'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
结论
我从中推断出,在我找不到的某个地方,在注册时,该程序正在尝试将新用户的 GUID 分配给 Id
,但是,数据库正在尝试保存它作为 UserId
。
如果我能得到更多帮助来解决这个问题,我将不胜感激。
编辑
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection" , throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
// modelBuilder.Entity<ApplicationUser>()
// .Property(p => p.Id)
// .HasColumnName("UserId");
//}
}
最佳答案
这是我最终解决这个问题的方法,首先,我恢复了我的数据库并创建了一个新模型。
然后我在 nugget 包管理器中更新了我的身份模型。然后我运行了该应用程序,它给了我一堆关于缺少列的错误。关键点,上次我尝试这样做时,我在 SQL 中手动创建了列,然后更新了我的模式,这可能是也可能不是我的失败。
我运行了启用数据库命令和更新命令来生成 sql 查询。
在我的例子中,生成的 SQL 是出于某种原因......错误的,它基本上创建了我已经拥有的表并且没有解决缺失的列,所以运行它当然会出错。
使用此链接 Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table以及 Chris Searles 的回答,我将 UP() 和 Down() 函数中的代码更改为他的代码,这就是解决我的问题的原因。
我的问题可能对我来说是独一无二的,但希望它能帮助别人。
关于c# - await UserManager.CreateAsync 查找不存在的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956235/
我使用以下代码来更改用户的密码: UserManager userManager = new UserManager(new UserStore(new ApplicationDbContext())
我正在尝试实现 Asp.Net Identity 2.0。到目前为止,我在 this blog 的帮助下管理得很好.但我走了一条稍微不同的路。我希望某些数据不属于 User 对象,而是属于新创建的 C
这个问题在这里已经有了答案: How would I run an async Task method synchronously? (25 个回答) 3个月前关闭。 在我的其中之一 IDatabas
我必须为我的单元测试创建 owin 环境,这是创建 Usermanager 对象所必需的。在我目前的情况下,我的 usermanager 为空,因为它没有获取 owincontext。 这是我访问
我有以下代码: var clientId = this.UserManager.FindById(this.User.Identity.GetUserId()).ClientId; 它是我应用程序中
我有一个问题,当我创建用户时,它没有初始化 list<> 类型的属性 - 我收到以下错误: An unhandled exception occurred while processing the r
我有一个应用程序使用 Microsoft.AspNetCore.Identity.UserManager 来管理用户。我使用核心 2.2。 UserManager 是通过我的服务中的内置依赖项注入(i
使用 ASP.NET Core 2。 我正在尝试初始化一些应用程序范围的数据。为此,我创建了一个名为 DbInitializer 的新类。 ,一个名为 IDbInitializer 的接口(inter
我的应用程序的所有身份验证和授权过程都是使用存储过程完成的。我已经编写了一个包含我需要的所有功能的类,例如GetUsers、Login、AddRole、AddMember 等。此外,用于管理用户、角色
UserManager.FindAsync 总能找到匹配项,即使我输入了错误的用户名和密码,它也总是会返回某种用户 ID。 [HttpPost] public ActionResult Mobilel
ASPNET Identity 中的 UserManager 包含 AddPasswordAsync 和 SetPhoneNumberAsync 等函数。 我已经为 FirstName 和 LastN
背景 我正在使用 .Net Core API 来驱动歌词应用程序。用户可以注册、提交艺术家和歌词,并在此过程中获得荣誉/XP 积分。基本上是一个社区驱动的歌词网站。 代码 这是我的 ArtistCon
所以我创建了这个继承自 UserManager 的用户服务,它看起来像这样: /// /// Service for handling users /// public class UserSer
我想做的是添加一个新的管理员用户并为其分配管理员角色。所以..我转到 Configure 方法中的 Startup.cs 类并编写了以下代码: var context = app.Applicatio
我有一个带有自定义电子邮件服务的自定义用户管理器。我在 AccountController 中调用 UserManager.SendEmailAsync() 方法,它只是挂起。我什至尝试将它与无效的
Android 7.1 UserManager类中出现的这个新方法描述here Checks if the calling app is running in a demo user. When ru
我正在使用 FOSUserBundle 并创建了一个自己的模型,其中包含一些新字段(例如 deviceVersion ( string ) 和 deviceType ( string )。 如果我创建
尝试使用 UserManager CreateAsync 方法创建新用户时出现以下错误。我使用的是未修改的 IdentityUser 和 IdentityRole。我有一些 DBSets,它们填充了数
我有一些服务负责在使用 UserManager 时更改用户密码并且一切正常,但是当我想编写一些测试时,它开始在方法 CheckPassword,它主要检查 current (old password)
如何在 ASP.NET Identity 中模拟 UserManager.GetRoles()?我知道它是一个扩展方法,所以我不能直接模拟它,也找不到需要模拟的底层方法/属性。 过去,我能够通过模拟
我是一名优秀的程序员,十分优秀!