2013-12-13 26 views
1

刚刚开始使用VS2013中的ASP.Net标识。我启动了一个MVC5项目,并使用它的默认模板和个人帐户。我想延长其从IdentityUser枝条以下字段继承了ApplicationUser类:使用导航属性扩展ASP.Net标识

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

我曾与文本框的名称和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); 
    } 

回答

0

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

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

+1

如果您发布了功能代码,您将获得+1 – Pascal

0

你需要一个SellingLocationId ApplicationUser和配置外键关系。当你创建一个新的ApplicationUser您只需填写SellingLocationId,而不是虚拟对象位置位置填入EF后插入或当您查询新的应用程序用户

此外,对于SellingLocation在数据库中复制必须意味着SellingLocationID不是一个唯一的ID。它需要。

+0

从我这里有点不清楚,重复的行有一个不同的Id,其余的是重复的数据。我和其他两个实体一起做了这件事,EF设法做出我想要的东西。在我的代码优先模型中放入fk和pk的好处是,但是我认为当添加一个名为Id的字段时,它会自动将它构造为主键。感谢您的回复。 – tobjak75

+0

对fk和pk进行了另一次检查,所有fk和pk都在那里,因为我使用了导航属性并将pk命名为Id。这可能是身份实体框架实施中的一个错误吗? – tobjak75

+0

您是否已将SellingLocationId添加到ApplicationUser,并将其设置为新的ApplicationUser而不是Location,如我所建议的那样。 –