2016-05-25 96 views
1

在我的控制器POST期間,如何更新我的USERS實體,然後使用新創建的UserID向我的USERROLES表中的角色分配角色。以下是我目前的代碼,嘗試保存第二組更改時發生錯誤。如何執行db.SaveChanges()然後再次更新數據庫?

我的USERROLES表由它們的UserIDRoleID的組合組成。在插入表格中時,自動生成的USERS

如果有人能告訴我如何實際解決這個問題,這將是太棒了 - 我仍然很新的ASP MVC。

using (var db = new SwinTCSEntities()) // initialise db object 
{ 
    // add values to USERS 
    db.USERS.Add(new USERS 
    { 
     FirstName = model.FirstName, 
     LastName = model.LastName, 
     Username = model.Username, 
     Password = Crypto.HashPassword("pass1234"), 
     Email = model.Email, 
     OfficeNo = model.OfficeNo, 
     PhoneNo = model.PhoneNo, 
     RegistrationDateTime = DateTime.Now, 
     MeetingNotifications = true, 
     SubmissionNotifications = true, 
     TeamAssignNotifications = true, 
     OverdueNotifications = true 
    }); 
    db.SaveChanges(); // this goes through fine 

    if (db.USERS.Any(x => x.Username == model.Username)) // it's able to get the new user value 
    { 
     if (model.IsAdmin && !model.IsStudent) 
     { 
      db.USERROLES.Add(new USERROLES 
      { 
       UserID = db.USERS.First(x => x.Username == model.Username).UserID, 
       RoleID = db.ROLES.First(x => x.Name == "Admin").RoleID, 
       DateAdded = DateTime.Now 
      }); 
      // It errors here with a concurrency error 
      db.SaveChanges(); 
     } 
    } 
} 

回答

0

不要使用調用SaveChanges 2次,我可以顯示anothers解決方案:1。 我變薄,因此新用戶擁有唯一的ID是嗎?所以使用try和catch方法或財產以後這樣的:

var user = new ApplicationUser { UserName = userViewModel.Email, Email = userViewModel.Email }; 
      var adminresult = await UserManager.CreateAsync(user, userViewModel.Password); 

      //Add User to the selected Roles 
      if (adminresult.Succeeded) 
      { 
       if (selectedRoles != null) 
       { 
        var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles); 
        if (!result.Succeeded) 
        { 
         ModelState.AddModelError("", result.Errors.First()); 
         ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name"); 
         return View(); 
        } 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", adminresult.Errors.First()); 
       ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name"); 
       return View(); 

      } 
      return RedirectToAction("Index"); 

,因爲你需要,我的英語方法對不起,您可以更改選定的角色。

1
using (var db = new SwinTCSEntities()) // initialise db object 
{ 
    // create USERS object 
    var user = new USERS 
    { 
     FirstName = model.FirstName, 
     LastName = model.LastName, 
     Username = model.Username, 
     Password = Crypto.HashPassword("pass1234"), 
     Email = model.Email, 
     OfficeNo = model.OfficeNo, 
     PhoneNo = model.PhoneNo, 
     RegistrationDateTime = DateTime.Now, 
     MeetingNotifications = true, 
     SubmissionNotifications = true, 
     TeamAssignNotifications = true, 
     OverdueNotifications = true 
    }; 


    //load the admin role in the EF context and add it to this new user 
     if (model.IsAdmin && !model.IsStudent) 
     { 
      var adminRole = db.ROLES.First(x => x.Name == "Admin"); 
      user.USERROLES.Add(new USERROLES 
      { 
       Role = adminRole, 
       DateAdded = DateTime.Now 
      }); 
     } 


    //add the user to the USERS set 
    db.USERS.Add(user); 

    db.SaveChanges(); // EF is smart enough to link the entities on SaveChanges 

} 
+0

我到達db.SaveChanges()時,仍然會得到一個併發異常,當修改爲使用'用戶'對象時。有任何想法嗎? – Voltstriker

+0

你會得到什麼例外? –

相關問題