我正在尝试使用DbFirst方法在.net核心上创建用户注册表单。 我在数据库中创建了一个表。使用用户身份验证(内置身份)创建.net核心项目。然后我做了脚手架。用适当的值填充字段并发布请求后,出现以下错误:
处理请求时发生未处理的异常。
SqlException:无效的列名'AccessFailedCount'。无效的栏 名称为“ ConcurrencyStamp”。无效的列名“ LockoutEnabled”。无效 列名“ LockoutEnd”。无效的列名“ NormalizedEmail”。 无效的列名“ NormalizedUserName”。无效的列名 “密码哈希”。无效的列名“ PhoneNumber”。无效的列名 “电话号码已确认”。无效的列名“ SecurityStamp”。无效 列名称“ TwoFactorEnabled”。无效的列名“ UserName”。
DbUpdateException:更新条目时发生错误。看到 内部异常以获取详细信息。
上述字段位于名为IdentityUser(我从中继承的那个)的只读文件中。它是只读的,所以我不能从中删除任何字段。
这是我的注册IActionResult:
[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);
}
我正在猜测,问题在于 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
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();
}
}
更新:刚刚发现我在数据库中看不到身份表。我猜这是另一个问题。