在用户身份系统中添加自定义 table 时出错

Error in adding custom table in user identity system

我正在关注这篇文章以添加我的自定义 table。 http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html 在我的 AccountViewModels.cs 中,我尝试添加新的自定义 table(UserProfileInfo) 类似这样的内容---

  public class ApplicationUser : IdentityUser
{
    public string EmailID { get; set; }
    public virtual UserProfileInfo UserProfileInfo { get; set; }
}
public class UserProfileInfo
{
    public int Id { get; set; }
    public string City { get; set; }
    public string MobileNum { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public System.Data.Entity.DbSet<UserProfileInfo> UserProfileInfo { get; set; }

}

}

并且在我的帐户控制器的注册操作(Post 版本)中,我尝试像这样更新注册操作,但您可以在 city 和 mobileNum 的代码中看到, 它的陈述----- xxx.RegisterViewModel' 不包含 'City' 的定义,并且找不到接受类型 'xxx.RegisterViewModel' 的第一个参数的扩展方法 'City'....

  [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.UserName, EmailID = model.EmailID,
                UserProfileInfo = new UserProfileInfo 
                { City = model.City,
                  MobileNum = model.ModileNum
                }
            };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

我不知道怎么回事here.plzz提前帮我out.thanks

您已经创建了单独的 table - UserProfileInfo - 这不是 ApplicationUser 的一部分。 你要做的是:

  1. 创建新的 ApplicationUser
  2. 创建新的 UserProfileInfo
  3. link 彼此通过适当的导航 属性 或手动分配外键(取决于您的配置)

我看过那篇文章,因为你 mentioned.You 永远无法将城市和手机号码作为参数传递,因为你没有在注册视图模型中定义它们。

如果您只想创建另一个 table 并想将其保存到数据库中,那么您可以这样做------

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser() { UserName = model.UserName };
            user.HomeTown = model.HomeTown;
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

并将您的 Dbcontext 更改为类似这样的内容----

         public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<UserProfileInfo> UserProfileInfo { get; set; }
    }

您的应用程序用户 class 应该是这样的-----

      public class ApplicationUser : IdentityUser
  {
    public string HomeTown { get; set; }
    public virtual UserProfileInfo UserProfileInfo { get; set; }
   }