2013-12-13 27 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,如我所建議的那樣。 –