首页 > 其他分享 >WPF中使用EntityFramework6和Identity

WPF中使用EntityFramework6和Identity

时间:2023-05-28 12:22:41浏览次数:37  
标签:MessageBox set Show get new WPF EntityFramework6 public Identity

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 = "[email protected]",
                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


 

 




标签:MessageBox,set,Show,get,new,WPF,EntityFramework6,public,Identity
From: https://www.cnblogs.com/niunan/p/17438071.html

相关文章

  • WPF入门教程系列二十六——DataGrid使用示例(3)
    WPF入门教程系列目录WPF入门教程系列二——Application介绍WPF入门教程系列三——Application介绍(续)WPF入门教程系列四——Dispatcher介绍WPF入门教程系列五——Window介绍WPF入门教程系列十一——依赖属性(一)WPF入门教程系列十五——WPF中的数据绑定(一) 五、DataGr......
  • 读取WINCC归档数据-WPF程序-2
       读取WINCC归档数据-可以读取实时WINCC数据和以往的数据,本文介绍读取实时数据的方法。  读取实际运行PLC的方法要简单些,这里介绍的是仿真读取。  1.运行博图并仿真程序 2.运行WINCC 3.用MicrosoftSQLServerManagementStudio18监视连接  ......
  • WPF 设置圆角窗体,通过ListView模拟下拉组合款
    界面:<Windowx:Class="WpfApp2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micros......
  • WPF入门实例 WPF完整例子 WPF DEMO WPF学习完整例子 WPF实战例子 WPF sql实例应用 WPF
    WPF和WinForms都是用于创建Windows桌面应用程序的开发框架,它们有一些相似之处,但也有很多不同之处。在开发速度方面,这取决于具体情况。如果您熟悉WinForms开发并且正在开发简单的界面应用程序,则可能会比使用WPF更快速地完成任务。然而,在设计和实现复杂的用户界面时,WPF可......
  • Wpf基础入门——容器Panel篇
    WPF布局基础WPF的布局原则一个窗口中只能包含一个元素不应显示设置元素尺寸不应使用坐标设置元素的位置可以嵌套布局容器WPF有哪些布局容器?Grid:网格。可以自定义行和列并通过行列的数量、行高和列宽来调整控件的布局。近似于HTML中的Table。StackPanel:栈式面板。可将......
  • Cisco Identity Services Engine (ISE) 3.2 Patch2 发布 - 思科身份服务引擎
    CiscoIdentityServicesEngine(ISE)3.2Patch2发布-思科身份服务引擎请访问原文链接:https://sysin.org/blog/cisco-ise-3/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org工作场所零信任安全的核心所在任何零信任策略的一个关键组成部分是确保所有人和所有设......
  • WPF三层架构的搭建
    小编首先来给大家介绍一下,什么是三层架构:三层架构就是为了符合“高内聚,低耦合”思想,把各个功能模块划分为表示层(UI)、业务逻辑层(BLL)和数据访问层(DAL)三层架构,各层之间采用接口相互访问,并通过对象模型的实体类(Model)作为数据传递的载体,不同的对象模型的实体类一般对应于数据库的不同表,......
  • 【盘点】界面控件DevExpress WPF的几大应用程序主题
    DevExpressWPF控件包含了50+个应用程序主题和40个调色板,用户可以在发布应用程序是指定主题,或允许最终用户动态修改WPF应用程序的外观和样式,其中主题带有调色板,可以进一步个性化您的UI!PS:DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。......
  • JPA通用策略生成器(@GeneratedValue 四种标准用法为TABLE, SEQUENCE, IDENTITY, AUTO)
    JPA通用策略生成器查看JPA的源码可知:packagejavax.persistence;/***Definesthetypesofprimarykeygenerationstrategies.**@seeGeneratedValue**@sinceJavaPersistence1.0*/publicenumGenerationType{/***Indicatesthatthepers......
  • WPF 使用Background="Transparent"+AllowsTransparency="True"实现穿透效果,窗体多次渲
    如果在WPF中的窗体使用AllowsTransparency="True"实现穿透效果,那么该窗体如果移动、快速渲染、控件比较多的情况,会出现卡顿,CPU暴涨的问题。基于以上情况,可以使用另一种方式实现,由@wuty@terryK指导:usingSystem.Windows;usingAnnotation.Business;namespaceDemo{//......