我有一个MVC 5应用,该应用在SQL Server数据库上使用Core Identity。在用户注册视图上,我想为分配用户的公司添加一个外键下拉列表。这是必填字段,用户只能分配给一个公司。
public class ApplicationUser : IdentityUser
{
....
public int? CompanyID { get; set; }
// [ForeignKey("CompanyId")]
// public virtual Company UserCompany { get; set; }
}
我已将CompanyID添加到AccountViewModels.cs下的RegisterViewModel中>
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(); }
然后最后我将下拉列表添加到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>
[运行应用程序并尝试注册新用户时,出现以下错误:
尝试登录时,在不同的行上出现相同的错误:
“ /”应用程序中的服务器错误。模型支持自数据库启动以来,“ ApplicationDbContext”上下文已更改创建。考虑使用代码优先迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
怎么了?
我有一个MVC 5应用,该应用在SQL Server数据库上使用Core Identity。在用户注册视图上,我想为分配用户的公司添加一个外键下拉列表。这是一个...
在程序包管理器控制台中尝试:
[谢谢Jansen,您的回答使我走上了正确的轨道,但并未完全解决问题。解决此问题的方法是将以下内容添加到Application_Start下的Global.asax文件中: