实体框架启用迁移 – 启用自动迁移

本文关键字:启用 迁移 实体 框架 | 更新日期: 2023-09-27 17:57:25

我正在处理表单身份验证,其中我使用了自己的实体框架,并且还使用DataContext为用户提供了角色。我已经硬编码了数据控制初始值设定项,如下所示

public class DataContextInitializer : CreateDatabaseIfNotExists { 受保护的覆盖无效种子(数据上下文上下文) { 角色 角色 1 = 新角色 { 角色名称 = "管理员" }; 角色角色 角色2 = 新角色 { 角色名称 = "用户" };

        User user1 = new User { Username = "admin", EmailId = "admin@ymail.com", FirstName = "Admin", Password = "123456", IsActive = true, DateCreated = DateTime.UtcNow, Roles = new List<Roles>() };
        User user2 = new User { Username = "user1", EmailId = "user1@ymail.com", FirstName = "User1", Password = "123456", IsActive = true, DateCreated = DateTime.UtcNow, Roles = new List<Roles>() };
        user1.Roles.Add(role1);
        user2.Roles.Add(role2);
        context.Users.Add(user1);
        context.Users.Add(user2);
        context.SaveChanges();
    }
}

我的登录方法是:

[维基邮报] [允许匿名]

    public ActionResult Login(LoginViewModel model, string returnUrl=" ")
    {
        if (ModelState.IsValid)
        {
            var user1 = Context.Users.Where(u => u.Username == model.Username && u.Password == model.Password).FirstOrDefault();
            var user = ob.Users.Where(u => u.Username == model.Username && u.Password == model.Password).FirstOrDefault();
            if (user != null)
            {
                var roles = user.Roles.Select(m => m.RoleName).ToArray();
                CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                serializeModel.UserId = user.UserId;
                serializeModel.FirstName = user.FirstName;
                serializeModel.LastName = user.LastName;
                serializeModel.roles = roles;

                string userData = JsonConvert.SerializeObject(serializeModel);
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, user1.EmailId, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData);
                string encTicket = FormsAuthentication.Encrypt(authTicket);
                HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                Response.Cookies.Add(faCookie);
                if ( roles.Contains("Admin"))
                {
                    return RedirectToAction("Index", "Admin");
                }
                else if (roles.Contains("User"))
                {
                    return RedirectToAction("Index", "User");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }

我的模型类如下:

公共类用户 {

    public int UserId { get; set; }
    [Required]
    public String Username { get; set; }
    [Required]
    public String EmailId { get; set; }
    [Required]
    public String Password { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Boolean IsActive { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual ICollection<Roles> Roles { get; set; }
}

公共类角色 {

    [Key]
    public int RoleId { get; set; }
    [Required]
    public string RoleName { get; set; }
    public string Description { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

问题就像当我尝试使用我的用户名和密码登录时,需要从 DatContext 类检查角色。

但是当时我的角色不是来自DataContext类,它给出了ArgumentNullException。

实体框架启用迁移 – 启用自动迁移

请参阅本文 由 @Oliboy50 回答(实体框架 6 - 处理嵌套对象的加载) 这可能会有所帮助,您需要包含虚拟角色以便加载集合