我很新的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。
你真正的问题是什么?什么不起作用? – Rik
@Rik我不知道如何将用户数据插入到另一个数据库的另一个表中,我尝试的代码没有做任何事情,我尝试了一些我在另一个示例中看到的内容。我应该在哪里做INSERT INTO ...部分? –
查看本教程 - 我认为它是EF6 - 看看它是否可以帮助您添加数据:http://www.entityframeworktutorial.net/EntityFramework4.3/add-entity-using-dbcontext.aspx – EtherDragon