使用导航属性扩展ASP.Net标识

时间:2013-12-13 11:44:35

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-identity

刚刚开始在VS2013中使用ASP.Net Identity。我启动了一个MVC5项目,并使用个人帐户使用默认模板。我正在尝试扩展从IdentityUser继承的ApplicationUser类,其中包含以下字段:

//this is my code first entity
public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }
    public string EmailAddress { get; set; }
    public virtual SellingLocation Location { get; set; }
}

我已经使用Name和EmailAddress的文本框扩展了Register.cshtml,更新了registerviewmodel并添加了一个包含所有SellingLocation的列表,用它填充下拉列表。

public class RegisterViewModel
{
    [Required]
    [Display(Name = "Användarnamn")]
    public string UserName { get; set; }

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

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

    [Required]
    [Display(Name = "Säljarens namn")]
    public string Name { get; set; }

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

    [Display(Name = "Försäljningställe")]
    public int SellingLocationId { get; set; }

    public List<SellingLocationViewModel> SellingLocations { get; set; }
}

下拉列表的Register.cshtml代码:

<div class="form-group">
    @Html.LabelFor(m => m.SellingLocationId, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.DropDownListFor(m => m.SellingLocationId, new SelectList(Model.SellingLocations, "Id", "Name"))
    </div>
</div>

问题出在帐户控制器中。当我尝试添加这个新用户时,会创建一个SellingLocation的新副本(数据库中的一个新行),而不是仅仅将一个引用(外键)添加到下拉列表中所选值所指向的行。我做错了什么?

// GET: /Account/Register
    [AllowAnonymous]
    public ActionResult Register()
    {
        var regvm = new RegisterViewModel();
        regvm.SellingLocations = GetSellingLocations();
        return View(regvm);
    }
    private List<SellingLocationViewModel> GetSellingLocations()
    {
        return Mapper.Map<List<SellingLocation>, List<SellingLocationViewModel>>(db.SellingLocations.ToList());
    }

        //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            //get the SellingLocation from the id from the selected value in dropdown
            var sellingloc = db.SellingLocations.Find(model.SellingLocationId);
            //creating user object, adding new data
            var user = new ApplicationUser() { UserName = model.UserName, Name = model.Name, EmailAddress = model.EmailAddress, Location = sellingloc };
            var result = await UserManager.CreateAsync(user, model.Password);
            //this will create a new row in SellingLocations, not just add a reference in AspNetUsers to the old row as expected.
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

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

2 个答案:

答案 0 :(得分:0)

您需要 ApplicationUser 中的 SellingLocationId 并配置您的外键关系。当您创建新的 ApplicationUser 时,您只需填写 SellingLocationId ,而不是虚拟对象 位置 即可。在插入后或查询新的 ApplicationUser 时,EF会填写 位置

此外,要在数据库中复制 SellingLocation ,必须意味着 SellingLocationID 不是唯一身份。它需要。

答案 1 :(得分:0)

我发现了问题。我使用了两个不同的上下文对象,一个用于UserManager,另一个用于在Register post方法中查找SellingLocation对象。

更改AccountController的构造函数并捕获传递给UserStore的上下文的引用,该上下文传递给UserManager修复了问题。当db对象是UserManager中对上下文的引用时,问题中的代码现在可以工作。

相关问题