本文介绍了将列添加到AspNetUsers表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作站设置:

  • Windows 8.1专业版
  • Visual Studio Pro 2013 v 12.0.40629.00更新5
  • .net v 4.6.01055

背景:

我使用个人帐户身份验证创建了我的项目,然后按照在Youtube上找到的指南,从现有数据库中创建了EDMX数据模型.我这样做是为了为我的网站创建身份验证.然后,我修改了web.config文件中的默认连接"以指向我现有的数据库.修改默认连接后,当我注册第一个用户帐户时,它会自动在现有数据库中生成所有必需的AspNet表.

I created my project using Individual Account Authentication and then following a guide I found on Youtube I created an EDMX Data Model from an existing database. I did this to create authentication for my website. I then modified the Default Connection in the web.config file to point to my existing database. When I registered the first user account after modifying the Default Connection it auto-generated all the necessary AspNet tables in my existing database.

问题:

我需要向AspNetUsers表添加一个自定义列.我按照Microsoft的指南进行操作,发现此处,但是当我进入修改Models \ IdentityModels.cs文件的步骤时,由于文件丢失,我无法这样做.

I need to add a custom column to the AspNetUsers table. I followed Microsoft's guide, found here, but when I get to the step about modifying the Models\IdentityModels.cs file I am unable to do so because the file is missing.

尝试解决的问题:

我试图通过简单地自己将列添加到SQL数据库中,然后编辑我的WebAPI代码来绕过此操作.

I've attempted to bypass this by simply adding the column into the SQL database myself and then editing my WebAPI code.

首先,我编辑了AccountBindingModels.cs文件中的RegisterBindingModel类.我添加了PeopleID部分.

First I edited the RegisterBindingModel class found in the AccountBindingModels.cs file. I added the PeopleID section.

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

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

    [Required]
    [Display(Name = "PeopleID")]
    public string PeopleID { 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; }
}

然后,我编辑了AccountController.cs文件中的Register类.我添加了与PeopleID有关的行.

Then I edited the Register class found in the AccountController.cs file. I added the line pertaining to PeopleID.

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

        IdentityUser user = new IdentityUser
        {
            UserName = model.UserName,
            Email = model.Email,
            PeopleID = model.PeopleID,
        };

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

        if (errorResult != null)
        {
            return errorResult;
        }

        return Ok();
    }

现在,一旦添加PeopleID = model.PeopleID,行,我将收到一条错误消息,指出"Microsoft.AspNet.Identity.EntityFramework.IdentityUser'不包含'PeopleID'的定义".当我转到IdentityUser.cs文件时,该文件已被锁定,不允许我对其进行编辑.

Now once I add the PeopleID = model.PeopleID, line I get an error message which states "Microsoft.AspNet.Identity.EntityFramework.IdentityUser' does not contain a definition for 'PeopleID'". When I go to the IdentityUser.cs file it is locked and will not allow me to edit it.

推荐答案

我最终重新启动了项目.在进行任何更改之前,我已更新了所有内容,并且未创建EDMX模型.我只是简单地将默认连接字符串更改为指向我的实时数据库.完成所有操作后,我找到了IdentityModels.cs文件,然后可以按照本指南对其进行修改:

I ended up restarting my project. I updated everything before I made any changes and did not create an EDMX model. I simply changed the default connection string to point to my live database. Once I did all that I found the IdentityModels.cs file and was then able to modify it following this guide:

https://blogs.msdn.microsoft.com/webdev/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/

这篇关于将列添加到AspNetUsers表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:33