C# 等待UserManager.CreateAsync查找不存在的列
形势 我有一个工作程序,我需要更新它来更新身份/登录方法。所以我从Identity 1更新到version2,并且我一直在修复错误,但我现在很为难 这是我的注册码C# 等待UserManager.CreateAsync查找不存在的列,c#,asp.net,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Identity,形势 我有一个工作程序,我需要更新它来更新身份/登录方法。所以我从Identity 1更新到version2,并且我一直在修复错误,但我现在很为难 这是我的注册码 公共异步任务寄存器(RegisterViewModel模型) { if(ModelState.IsValid) { //试一试 //{ var user=new ApplicationUser(){UserName=model.Email,Email=model.Email}; var result=await UserManager
公共异步任务寄存器(RegisterViewModel模型)
{
if(ModelState.IsValid)
{
//试一试
//{
var user=new ApplicationUser(){UserName=model.Email,Email=model.Email};
var result=await UserManager.CreateAsync(用户、模型、密码);
if(result.successed)
问题
出现的问题是,我在第var result=wait UserManager.CreateAsync(user,model.Password);
行上获得了一个无效的列名“UserId”
我不完全确定为什么会发生这种情况(或者这个UserId
来自哪里,可能来自旧标识?),因为当我在行上放置断点时,SQL将查询显示为
选择
[Extent1].[Id]作为[Id],
[Extent1].[Email]作为[Email],
[Extent1]。[EmailConfirmed]作为[EmailConfirmed],
[Extent1]。[PasswordHash]作为[PasswordHash],
[Extent1]。[SecurityStamp]作为[SecurityStamp],
[Extent1]。[PhoneNumber]作为[PhoneNumber],
[Extent1]。[PhoneNumberConfiged]作为[PhoneNumberConfiged],
[Extent1][TwoFactorEnabled]作为[TwoFactorEnabled],
[Extent1]。[LockoutEndDateUtc]作为[LockoutEndDateUtc],
[Extent1]。[LockoutEnabled]作为[LockoutEnabled],
[Extent1]。[AccessFailedCount]作为[AccessFailedCount],
[Extent1].[UserName]作为[UserName]
来自[dbo].[AspNetUsers]作为[Extent1]
我通过在AspNetUsers表中添加UserId
来调节程序,结果发生了两件事
Id
重命名为UserId
并生成一个普通字段Id时。
然后我得到的错误是
InnerException={“无法将值NULL插入到表“dbo.AspNetUsers”的列“UserId”中;列不允许为NULL。insert失败。\r\n语句已终止。“}
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");
//}
}
//您可以通过向ApplicationUser类添加更多属性来为用户添加配置文件数据,请访问https://go.microsoft.com/fwlink/?LinkID=317594 了解更多。
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
//模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
//{
//基于模型创建(modelBuilder);
//modelBuilder.Entity()
//.Property(p=>p.Id)
//.HasColumnName(“用户ID”);
//}
}
以下是我最终解决这个问题的方法,
首先,我恢复了我的数据库并做了一个新的模型
然后,我在nugget package manager中更新了我的身份模型。
然后我运行了这个应用程序,它给了我一大堆关于缺少列的错误。
关键点,上次尝试此操作时,我在SQL中手动创建了列,然后更新了我的模式,这可能是我的失败,也可能不是
遵循此链接
我运行了Enable database命令和update命令来生成sql查询
在我的例子中,生成的SQL是出于某种原因……错误的,它基本上创建了我已经拥有的表,并且没有处理缺少的列,因此运行它当然会产生错误
使用这个链接和Chris Searles的答案,我将UP()和Down()函数中的代码更改为他的,这就是解决我问题的方法
我的问题对我来说可能是独一无二的,但希望它能帮助到其他人。您是先使用代码吗?能否将您的自定义
IdentityDbContext
?尤其是ApplicationUser
作为well@Dongdong我已将其添加到文件中,我相信是数据库先添加的。因此,您现在没有在ModelCreating上使用,
?看起来您更改了co列名称到UserId
(HasColumnName(“UserId”)
),但没有使用数据库迁移代码管理它。请遵循此官方迁移帖子,以清除数据库,使其与您的代码完全相同。没有,当它要求UserId
时,我在测试时尝试了此操作,但未成功。因此,我在MS SQL中手动创建了该字段以进行测试。