软件开发培训学校:ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作( 二 )

数据库上下文类是为给定数据模型协调 EF Core 功能的主类 。上下文派生自 Microsoft.EntityFrameworkCore.DbContext 。上下文指定数据模型中包含哪些实体 。在此项目中将数据库上下文类命名为 SchoolUserInfoContext 。
options) : base(options) { } /// public DbSet /// DbSet实体集属性对应数据库中的表(注意实体集名必须与表明一致) /// UserInfos { get; set; } /// /// protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity /// TODO:当数据库创建完成后 ,  EF 创建一系列数据表 , 表名默认和 DbSet 属性名相同 。集合属性的名称一般使用复数形式 , 但不同的开发人员的命名习惯可能不一样 ,  /// 开发人员根据自己的情况确定是否使用复数形式 。在定义 DbSet 属性的代码之后 , 添加下面代码 , 对DbContext指定单数的表名来覆盖默认的表名 。///.ToTable("UserInfo"); } }}回到顶部六、将上下文添加到 Startup.cs 中的依赖项注入: // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //注入EF Core数据库上下文服务 services.AddDbContext(options => options.UseMySQL(Configuration.GetConnectionString("MySqlConnection"))); services.AddControllersWithViews; }回到顶部七:引入Layui样式和js:前往官网下载Layui相关样式和js包 , 下载地址:https://www.layui.com/Layui弹出层插件layer.js(有很多地方需要用到弹窗) , 下载地址:https://layer.layui.com/将相关文件存放到wwwroot文件下:将相关文件引入默认布局页面中:
软件开发培训学校:ASP.NET Core MVC+Layui使用EF Core连接MySQL执行简单的CRUD操作

文章插图
 回到顶部八、 ASP.NET Core MVC 和 EF Core实现MySQL CRUD功能:
注意在这里主要展示的EF Core与数据库操作的部分代码 , 详细代码可下载实例源码查看 。
/// 学生信息添加 /// Create(AddUserInfoViewModel addUserInfo) { try { var userInfo=new UserInfo { UserName = addUserInfo.UserName, Sex = addUserInfo.Sex, Hobby = addUserInfo.Hobby, Phone = addUserInfo.Phone, Deion = addUserInfo.Deion }; _shoSchoolUserInfoContext.UserInfos.Add(userInfo); await _shoSchoolUserInfoContext.SaveChangesAsync; return true; } catch { return false; } } Retrieve: /// /// 当前页码 /// 显示条数 /// 用户姓名 /// public async Task /// 获取用户信息 /// GetPageListData(int page = 1, int limit = 15, string userName = "") { try { List listData; var totalCount = 0; if (!string.IsNullOrWhiteSpace(userName)) { listData = http://www.chuangnv.com/post/await _shoSchoolUserInfoContext.UserInfos.Where(x => x.UserName.Contains(userName)).OrderByDescending(x => x.Id).Skip((page - 1) * limit).Take(limit).ToListAsync; totalCount = _shoSchoolUserInfoContext.UserInfos .Count(x => x.UserName.Contains(userName)); } else { listData = await _shoSchoolUserInfoContext.UserInfos.OrderByDescending(x => x.Id).Skip((page - 1) * limit).Take(limit).ToListAsync; totalCount = _shoSchoolUserInfoContext.UserInfos.Count; } return new PageSearchModel { ResultMsg = "success", Code = 200, TotalCount = totalCount, DataList = listData }; } catch (Exception e) { return new PageSearchModel { Code = 400, ResultMsg = e.Message }; } } Update: /// /// /// public async Task /// 学生信息修改 /// Update(UserInfo userInfo) { try { _shoSchoolUserInfoContext.UserInfos.Update(userInfo); await _shoSchoolUserInfoContext.SaveChangesAsync; return true; } catch { return false; } } Delete: /// /// /// public async Task /// 学生信息删除 /// Delete(int? id) { try { var searchUserInfo = await _shoSchoolUserInfoContext.UserInfos.FindAsync(id); if (searchUserInfo == null) { return false; } _shoSchoolUserInfoContext.UserInfos.Remove(searchUserInfo); await _shoSchoolUserInfoContext.SaveChangesAsync; return true; } catch { return false; } }


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: