$http post 在向内置用户添加属性后导致 C# MVC 出现 500(内部服务器错误)

$http post resulting in 500 (Internal Server Error) with C# MVC after adding properties to built in User

我在尝试使用 ASP.net 的内置用户身份验证注册新用户时收到 500 内部服务器错误。

这以前对我有用,但现在我尝试将 FirstNameLastName 字段添加到 'AspNetUser' table 并且我收到了500 错误。

在我的 app.js 中,我使用 angular 与我的 c sharp 控制器进行通信。自应用程序正确注册用户以来,我在添加的所有代码旁边都添加了带有“**”的注释。

之前我只注册了 emailpasswordconfirmPassword 的用户。尝试添加 FirstNameLastName 时,出现 500 错误。

JS:

$http.post(API_END_POINT + '/api/Account/Register', user);  //**added to post the new user. this call is triggered when a header is clicked in my html

var user = {
    Email: 'Joe@gmail.com',
    Password: 'Pass1234!',
    ConfirmPassword: 'Pass1234!',
    FirstName: 'Joe',       ///**added FirstName and LastName to test JS object
    LastName: 'Schmo'       ///**
}

账户控制器

    //[Authorize]
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{

        // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register/")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName}; //**added FirstName and LastName to ApplicationUser

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();
    }

}

IdentityModels.cs

我尝试遵循 /u/dima from this accepted answer as well as /u/ Alex Polyankin in the question I posted earlier today 的建议,将 FirstNameLastName 属性添加到 ApplicationUser

    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }

    public string FirstName { get; set; }     //**added these per user Dima's suggestion
    public string LastName { get; set; }      //**

}

AccountBindingModels.cs

 public class RegisterBindingModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }     //**added FirstName to  account binding class

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }       //**added FirstName to  account binding class

}

同样,我对我添加的所有代码都添加了 ** 评论。

调试时,用户(名字和姓氏)appears to make it to the controller properly

然而this is as far as I make it in the debugger。我在帐户控制器中输入最后一个 if 语句之前收到 500 错误。

here是调试器中的实际错误。

可能导致此错误的原因是什么?

非常感谢您抽出宝贵时间。如果您需要任何其他信息或者我不清楚,请告诉我。

从上面的评论来看,很明显是数据库迁移错误。包管理器控制台代码:

enable-migrations
add-migration InitialCreate
update-database

有关示例的更多信息 here