2016-07-26 96 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

这终于做到了,谢谢 –

相关问题