我很新的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