WPF中使用EntityFramework6和Identity
2023年05月28日 VS2022 WFP .NETFramework,Version=v4.6.2 测试通过
NUGET:EntityFramework 6.4.4 , Microsoft.AspNet.Identity.EntityFramework 2.2.3
1 2 3 4 5 6 | //扩展内置的identityuser public class ApplicationUser : IdentityUser { public string Sex { set ; get ; } = "未知" ; public string City { set ; get ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //自己定义的实体类 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中
1 2 3 | <connectionStrings> <add name= "DefaultConnection" connectionString= "server=.\sqlexpress;uid=sa;pwd=Niunan123456.;database=mvcapp1ef;" providerName= "System.Data.SqlClient" /> </connectionStrings> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | //增加普通实体类,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 |
撸码:复制、粘贴,拿起键盘就是“干”!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-05-28 很好看的后台管理界面