在用户标识系统中添加自定义表时出错



我按照本文添加我的自定义表。http://www.itorian.com/2013/11/customizing-users-profile-to-add-new.html在我的AccountViewModels.cs中,我试图添加新的自定义表(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; }
}

}

和在我的帐户控制器的注册操作(后版本),我已经尝试更新注册操作像这样,但你可以看到在城市和mobileNum的代码,它的陈述----- xxx。RegisterViewModel'不包含'City'的定义,也没有扩展方法'City'接受类型为'xxx '的第一个参数。可以找到RegisterViewModel' ....

  [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);
    }

我不知道这里发生了什么。请帮帮我。提前感谢

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

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

    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; }
    }

和您的应用程序用户类应该像这样-----

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

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

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

最新更新