C# ASP.NET MVC 5核心标识公司ID dbo.AspNetUsers表上的外键
我有一个MVC5应用程序,它在SQL server数据库上使用核心标识。在“用户注册”视图中,我想为用户分配到的公司添加外键下拉列表。这是必填字段,用户只能分配到一个公司 我已将CompanyID添加到AccountViewModels.cs下的RegisterViewModelC# ASP.NET MVC 5核心标识公司ID dbo.AspNetUsers表上的外键,c#,asp.net,sql-server,asp.net-mvc-5,asp.net-core-identity,C#,Asp.net,Sql Server,Asp.net Mvc 5,Asp.net Core Identity,我有一个MVC5应用程序,它在SQL server数据库上使用核心标识。在“用户注册”视图中,我想为用户分配到的公司添加外键下拉列表。这是必填字段,用户只能分配到一个公司 我已将CompanyID添加到AccountViewModels.cs下的RegisterViewModel public class RegisterViewModel { .... [Required] [Display(Name = "Company")] public int Co
public class RegisterViewModel
{
....
[Required]
[Display(Name = "Company")]
public int CompanyID { get; set; }
然后,我向AccountController添加了以下内容:
using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using EMS.Models;
namespace EMS.Controllers
{
// [Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private ApplicationDbContext context;
**private EMSEntities db = new EMSEntities();**
public AccountController()
{
context = new ApplicationDbContext();
}
//
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
**ViewBag.Company = new SelectList(db.Companies.ToList(), "ID", "CompanyName");**
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model, ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
**ViewBag.Company = new SelectList(db.Companies.ToList(), "ID", "CompanyName", model.CompanyID);**
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, **CompanyID = model.CompanyID** };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit https://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>");
await this.UserManager.AddToRoleAsync(user.Id, model.Name);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
// New Methods
[AllowAnonymous]
[HttpGet]
public ActionResult RegisterRole()
{
ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
ViewBag.UserName = new SelectList(context.Users.ToList(), "UserName", "UserName");
return View();
}
使用系统;
利用制度全球化;
使用System.Linq;
使用System.Security.Claims;
使用System.Threading.Tasks;
使用System.Web;
使用System.Web.Mvc;
使用Microsoft.AspNet.Identity;
使用Microsoft.AspNet.Identity.Owin;
使用Microsoft.Owin.Security;
使用EMS.Models;
名称空间EMS.Controllers
{
//[授权]
公共类AccountController:控制器
{
专用应用程序signInManager\u signInManager;
私有应用程序用户管理器\u用户管理器;
私有应用程序上下文上下文;
**私有emsenties db=新emsenties()**
公共账户控制员()
{
context=新的ApplicationDbContext();
}
//
//获取:/Account/注册
[异名]
公众行动结果登记册()
{
**ViewBag.Company=新选择列表(db.companys.ToList(),“ID”,“CompanyName”)**
ViewBag.Name=新选择列表(context.Roles.ToList(),“Name”,“Name”);
返回视图();
}
//
//职位:/Account/Register
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
等待这个.UserManager.AddToRoleAsync(user.Id,model.Name);
返回重定向到操作(“索引”、“主页”);
}
加法器(结果);
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
//新方法
[异名]
[HttpGet]
公共行动结果注册表项()
{
ViewBag.Name=新选择列表(context.Roles.ToList(),“Name”,“Name”);
ViewBag.UserName=新选择列表(context.Users.ToList(),“UserName”,“UserName”);
返回视图();
}
最后,我在Register视图中添加了下拉列表:
<div class="form-group">
@Html.Label("Company", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("CompanyID", null, htmlAttributes: new { @class = "form-control" })
</div>
</div>
@Label(“Company”,新的{@class=“col-md-2控制标签”})
@DropDownList(“CompanyID”,null,htmlAttributes:new{@class=“form control”})
运行应用程序并尝试注册新用户时,我遇到以下错误:
尝试登录时,我在另一行上遇到相同的错误:
“/”应用程序中出现服务器错误。支持
“ApplicationDbContext”上下文自数据库创建以来已更改
考虑使用代码第一迁移来更新数据库
()
问题出在哪里?在Package Manager控制台中尝试:
Enable-Migrations -ContextTypeName 'Applicationname.Data.ApplicationDbContext'
add-migration 'migrationsname'
update-database
它只需要更新数据库。这是因为代码在启用迁移之前首先创建了一个数据库,因此您将需要初始迁移。谢谢Jansen,您的回答让我走上了正确的轨道,但并没有完全解决问题。解决问题的方法是在application下的Global.asax文件中添加以下内容n_开始:
Database.SetInitializer<ApplicationDbContext>(null);
Database.SetInitializer(null);
Database.SetInitializer<ApplicationDbContext>(null);