Asp.net core &引用;DbUpdateException:更新条目时出错";实现自定义标识类后出错
我正在尝试使用DbFirst方法在.NETCore上创建用户注册表单。 我在数据库中创建了一个表。使用用户身份验证(内置标识)创建.net核心项目。然后我做了脚手架。在用正确的值填充字段并发布请求后,我收到以下错误: 处理请求时发生未处理的异常 SqlException:列名“AccessFailedCount”无效。无效列 名称为“ConcurrencyStamp”。列名“LockoutEnabled”无效。无效的 列名“LockoutEnd”。列名“NormalizedEmail”无效。 列名“NormalizedUserName”无效。无效的列名 “密码哈希”。无效的列名“PhoneNumber”。无效的列名 “电话号码已确认”。列名“SecurityStamp”无效。无效的 列名“TwoFactorEnabled”。列名“UserName”无效 DbUpdateException:更新条目时出错。看见 详细信息的内部异常 上面提到的字段位于名为IdentityUser(我从中继承的)的只读文件中。它是只读的,所以我不能从中删除任何字段 这是我的注册结果:Asp.net core &引用;DbUpdateException:更新条目时出错";实现自定义标识类后出错,asp.net-core,asp.net-identity,Asp.net Core,Asp.net Identity,我正在尝试使用DbFirst方法在.NETCore上创建用户注册表单。 我在数据库中创建了一个表。使用用户身份验证(内置标识)创建.net核心项目。然后我做了脚手架。在用正确的值填充字段并发布请求后,我收到以下错误: 处理请求时发生未处理的异常 SqlException:列名“AccessFailedCount”无效。无效列 名称为“ConcurrencyStamp”。列名“LockoutEnabled”无效。无效的 列名“LockoutEnd”。列名“NormalizedEmail”无效。 列
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
var errors = ModelState.Where(c => c.Value.Errors.Count > 0).Select(c => c.Value).ToList();
if (ModelState.IsValid)
{
var user = new User
{
Name = model.Name,
Email = model.Email,
Address = model.Address,
PersonalId = model.PersonalId,
Country = model.Country,
MobilePhone = model.MobilePhone,
BirthDate = model.BirthDate,
};
_context.User.Add(user);
_context.SaveChanges();
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Home", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
return RedirectToAction("RegisterConfirm");
}
}
return View(model);
}
[HttpPost]
公共异步任务寄存器(RegisterViewModel模型,字符串returnUrl=null)
{
ViewData[“ReturnUrl”]=ReturnUrl;
var errors=ModelState.Where(c=>c.Value.errors.Count>0);
if(ModelState.IsValid)
{
var user=新用户
{
Name=model.Name,
Email=model.Email,
地址=型号。地址,
PersonalId=model.PersonalId,
国家=型号。国家,
移动电话=model.MobilePhone,
生日=型号。生日,
};
_context.User.Add(用户);
_SaveChanges();
var result=await\u userManager.CreateAsync(用户、模型、密码);
if(result.successed)
{
var code=wait_userManager.GenerateEmailConfirmationTokenAsync(用户);
var callbackUrl=Url.Action(“确认邮件”,“主页”,新的{userId=user.Id,code=code},协议:HttpContext.Request.Scheme);
返回重定向到操作(“注册确认”);
}
}
返回视图(模型);
}
我猜,问题是IdentityUser中提到的字段是空的,这就是我得到错误的原因,但可能是我错了。
User.cs
public partial class User :IdentityUser<int>
{
public int Id { get; set; }
public string Country { get; set; }
public string PersonalId { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public byte[] Idimage { get; set; }
public bool? EmailConfirmed { get; set; }
public bool? Smsconfirmed { get; set; }
}
公共部分类用户:IdentityUser
{
公共int Id{get;set;}
公共字符串国家{get;set;}
公共字符串PersonalId{get;set;}
公共字符串移动电话{get;set;}
公共字符串电子邮件{get;set;}
公共日期时间出生日期{get;set;}
公共字符串密码{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字节[]Idimage{get;set;}
公共bool?电子邮件确认{get;set;}
公共bool?Smsconfirmed{get;set;}
}
识别码
namespace Microsoft.AspNetCore.Identity
{
//
// Summary:
// Represents a user in the identity system
//
// Type parameters:
// TKey:
// The type used for the primary key for the user.
public class IdentityUser<TKey> where TKey : IEquatable<TKey>
{
//
// Summary:
// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
public IdentityUser();
//
// Summary:
// Initializes a new instance of Microsoft.AspNetCore.Identity.IdentityUser`1.
//
// Parameters:
// userName:
// The user name.
public IdentityUser(string userName);
//
// Summary:
// Gets or sets the date and time, in UTC, when any user lockout ends.
//
// Remarks:
// A value in the past means the user is not locked out.
public virtual DateTimeOffset? LockoutEnd { get; set; }
//
// Summary:
// Gets or sets a flag indicating if two factor authentication is enabled for this
// user.
[PersonalData]
public virtual bool TwoFactorEnabled { get; set; }
//
// Summary:
// Gets or sets a flag indicating if a user has confirmed their telephone address.
[PersonalData]
public virtual bool PhoneNumberConfirmed { get; set; }
//
// Summary:
// Gets or sets a telephone number for the user.
[ProtectedPersonalData]
public virtual string PhoneNumber { get; set; }
//
// Summary:
// A random value that must change whenever a user is persisted to the store
public virtual string ConcurrencyStamp { get; set; }
//
// Summary:
// A random value that must change whenever a users credentials change (password
// changed, login removed)
public virtual string SecurityStamp { get; set; }
//
// Summary:
// Gets or sets a salted and hashed representation of the password for this user.
public virtual string PasswordHash { get; set; }
//
// Summary:
// Gets or sets a flag indicating if a user has confirmed their email address.
[PersonalData]
public virtual bool EmailConfirmed { get; set; }
//
// Summary:
// Gets or sets the normalized email address for this user.
public virtual string NormalizedEmail { get; set; }
//
// Summary:
// Gets or sets the email address for this user.
[ProtectedPersonalData]
public virtual string Email { get; set; }
//
// Summary:
// Gets or sets the normalized user name for this user.
public virtual string NormalizedUserName { get; set; }
//
// Summary:
// Gets or sets the user name for this user.
[ProtectedPersonalData]
public virtual string UserName { get; set; }
//
// Summary:
// Gets or sets the primary key for this user.
[PersonalData]
public virtual TKey Id { get; set; }
//
// Summary:
// Gets or sets a flag indicating if the user could be locked out.
public virtual bool LockoutEnabled { get; set; }
//
// Summary:
// Gets or sets the number of failed login attempts for the current user.
public virtual int AccessFailedCount { get; set; }
//
// Summary:
// Returns the username for this user.
public override string ToString();
}
}
名称空间Microsoft.AspNetCore.Identity
{
//
//总结:
//表示标识系统中的用户
//
//类型参数:
//TKey:
//用于用户主键的类型。
公共类标识,其中TKey:IEquatable
{
//
//总结:
//初始化Microsoft.AspNetCore.Identity.IdentityUser`1的新实例。
公共身份识别器();
//
//总结:
//初始化Microsoft.AspNetCore.Identity.IdentityUser`1的新实例。
//
//参数:
//用户名:
//用户名。
公共标识用户(字符串用户名);
//
//总结:
//获取或设置任何用户锁定结束的日期和时间(以UTC为单位)。
//
//备注:
//过去的值表示用户未被锁定。
公共虚拟DateTimeOffset?锁定输出{get;set;}
//
//总结:
//获取或设置一个标志,该标志指示是否为此启用了双因素身份验证
//用户。
[个人资料]
公共虚拟布尔TwoFactorEnabled{get;set;}
//
//总结:
//获取或设置一个标志,该标志指示用户是否已确认其电话地址。
[个人资料]
公共虚拟bool phonenumberconfirm{get;set;}
//
//总结:
//获取或设置用户的电话号码。
[受保护的个人数据]
公共虚拟字符串PhoneNumber{get;set;}
//
//总结:
//一个随机值,每当用户被持久化到存储时,该值必须更改
公共虚拟字符串并发amp{get;set;}
//
//总结:
//当用户凭据(密码)更改时必须更改的随机值
//已更改,已删除登录名)
公共虚拟字符串SecurityStamp{get;set;}
//
//总结:
//获取或设置此用户的密码的salt和hash表示形式。
公共虚拟字符串密码哈希{get;set;}
//
//总结:
//获取或设置一个标志,指示用户是否已确认其电子邮件地址。
[个人资料]
公共虚拟布尔值{get;set;}
//
//总结:
//获取或设置此用户的标准化电子邮件地址。
公共虚拟字符串NormalizedEmail{get;set;}
//
//总结:
//获取或设置此用户的电子邮件地址。
[受保护的个人数据]