2016-07-26 91 views
0

我很新的ASP.NET,特別是ASP.NET MVC,我目前需要修改註冊過程,通過使用戶成功註冊後,我用另一個數據庫的用戶電子郵件和用戶名插入其他表中。到目前爲止,我還沒有找到一種方法來實現這一點,因爲我發現的其他事情是添加字段來註冊或更改數據驗證,我目前不需要。ASP.NET MVC 5自定義用戶註冊連接到另一個數據庫

我創建了一個存儲過程,也是這樣,但我不知道哪個方法更好。

IdentityModel.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ApplicationDbContext() 
      : base("UsersConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ApplicationDbContext Create() 
     { 
      return new ApplicationDbContext(); 
     } 
    } 

    public class ZivoyDbContext : IdentityDbContext<ApplicationUser> 
    { 
     public ZivoyDbContext() 
      : base("DefaultConnection", throwIfV1Schema: false) 
     { 
     } 

     public static ZivoyDbContext Create() 
     { 
      return new ZivoyDbContext(); 
     } 
    } 

而且賬戶控制器

// POST: /Account/Register 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

        //Insert into another table 
        ZivoyDbContext.Users.Add(); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 
        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

        return RedirectToAction("Index", "Home"); 
       } 
       AddErrors(result); 
      } 

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

我甚至不知道如何做到這一點,請有人可以解釋我是如何工作的?我一直在尋找連接到SQL Server的MVC 5,但幾乎所有答案都對應於使用單個連接的單個控制器或使用ADO.NET。

+0

你真正的問題是什麼?什麼不起作用? – Rik

+0

@Rik我不知道如何將用戶數據插入到另一個數據庫的另一個表中,我嘗試的代碼沒有做任何事情,我嘗試了一些我在另一個示例中看到的內容。我應該在哪裏做INSERT INTO ...部分? –

+0

查看本教程 - 我認爲它是EF6 - 看看它是否可以幫助您添加數據:http://www.entityframeworktutorial.net/EntityFramework4.3/add-entity-using-dbcontext.aspx – EtherDragon

回答

0

我認爲問題在於您試圖通過ZivoyDbContext添加用戶,但不會將用戶數據傳遞到方法中。

只要ZivoyDbContext數據庫和表已經創建,使用類似這樣的電話:

// Create a new User object using (all of the required) properties of the 
// new User you have just created via the Register method 
ZivoyDbContext.Users.Add(new ApplicationUser 
       { 
        UserName = user.UserName, 
        PasswordHash = user.Password, 
        Email = user.Email 
       } 
); 

不忘的SaveChanges()如果不是在你的情況下自動完成...

請記住,IdentityDbContext與普通EF上下文無關。 你的上下文可能會有添加,更新,刪除,查找等方法來保持數據庫處於最新狀態。 IdentityDbContext有很多用於管理用戶及其登錄名的內容 - 如果您只想在其中存儲用戶的副本,則其他數據庫很可能不需要這些內容。 如果您沒有使用所有這些功能,那麼從IdentityDbContext獲得您的上下文繼承是有點過分的。

+0

這終於做到了,謝謝 –

相關問題