WPF中使用EntityFramework6和Identity

发布时间 2023-05-28 12:13:36作者: 牛腩

2023年05月28日 VS2022 WFP .NETFramework,Version=v4.6.2 测试通过

NUGET:EntityFramework 6.4.4 , Microsoft.AspNet.Identity.EntityFramework 2.2.3

 

 

//扩展内置的identityuser 
public class ApplicationUser : IdentityUser
    {
        public string Sex { set; get; } = "未知";
        public string City { set; get; }
    }
 

//自己定义的实体类  
public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

   public class AppDbContext : IdentityDbContext<ApplicationUser>
    {
        public AppDbContext ():base("DefaultConnection")
        { }
        public DbSet<Student> Students { get; set; }
    }

 

其中那个DefaultConnection在App.config中

 

 

	<connectionStrings>
		<add name="DefaultConnection" connectionString="server=.\sqlexpress;uid=sa;pwd=Niunan123456.;database=mvcapp1ef;" providerName="System.Data.SqlClient"/>
	</connectionStrings>

 

//增加普通实体类,identity用户,角色的代码
            AppDbContext _context = new AppDbContext();

            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(_context);
            UserManager<ApplicationUser> _userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>(_context);
            RoleManager<IdentityRole> _roleManager = new RoleManager<IdentityRole>(roleStore);
            #region 添加普通实体类
            //_context.Students.Add(new Models.Student()
            //{
            //    Name = "牛腩",
            //    Birthday = DateTime.Parse("1986-2-2")
            //});
            //int i = _context.SaveChanges();
            //MessageBox.Show("新增结果:" + i);
            #endregion
            #region 添加identity用户
            ApplicationUser u = new ApplicationUser()
            { 
                UserName = "juanjuan",
                Email = "juanjuan@niunan.net",
                City = "桂林"
            };
            IdentityResult res = _userManager.Create(u, "123456");
            if (res.Succeeded)
            {
                MessageBox.Show("新增identity用户成功,用户ID:" + u.Id);
            }
            else
            {
                MessageBox.Show("新增identity用户失败:" + res.Errors.ElementAt(0));
            }
            #endregion
            #region 登录并判断是否在某一角色中
            //string username = "niunan";
            //string password = "123456";

            //ApplicationUser u = _userManager.Find(username, password);
            //if (u != null)
            //{
            //    _userManager.AddToRole(u.Id, "Admin"); //把用户加入到角色当中
            //    bool b = _userManager.IsInRole(u.Id, "Admin");
            //    MessageBox.Show($"登录成功,用户:{username},密码:{password},是否在Admin角色中:{b}");
            //}
            //else
            //{
            //    MessageBox.Show($"用户登录失败");
            //}
            #endregion
            #region 新增角色
            //IdentityRole r = new IdentityRole() { Name = "Admin" };

            //IdentityResult res = _roleManager.Create(r);
            //if (res.Succeeded)
            //{
            //    MessageBox.Show("新增角色Admin成功");
            //}
            //else
            //{
            //    MessageBox.Show("新增角色Admin失败:" + res.Errors.ElementAt(0));
            //} 
            #endregion