[英]await UserManager.CreateAsync looking for column that does not exist
情況
我有一個我更新的工作程序需要更新身份/登錄方法。 所以我從 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);
Invalid column name '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 = {"無法將值 NULL 插入到列 'UserId',表 'dbo.AspNetUsers';列不允許空值。INSERT 失敗。\r\n語句已終止。"}
結論
我從中推斷的是,在我找不到的地方,在注冊時,程序正試圖將新用戶的 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");
//}
}
這是我最終解決這個問題的方法,首先,我恢復了我的數據庫並做了一個新的 model。
然后我在 nugget package 管理器中更新了我的身份 Model。 然后我運行了這個應用程序,它給了我一堆關於缺少列的錯誤。 關鍵點,上次我嘗試這樣做時,我在 SQL 中手動創建了列,然后更新了我的模式,這可能是也可能不是我的失敗。
我運行了啟用數據庫命令和更新命令來生成 sql 查詢。
就我而言,生成的 SQL 出於某種原因......錯誤,它基本上創建了我已經擁有的表並且沒有解決缺失的列,因此運行它當然會出錯。
使用此鏈接Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table和 Chris Searles 的答案,我將 UP() 和 Down() function 中的代碼更改為他的,這就是修復我的問題。
我的問題可能對我來說是獨一無二的,但希望它對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.