ASP.NET Core 入门笔记9,ASP.NET Core + Entity Framework Core 数据访问入门
一、前言
1、本教程主要内容
- ASP.NET Core MVC 集成 EF Core 介绍&操作步骤
- ASP.NET Core MVC 使用 EF Core + Linq to Entity 访问MySQL数据库
- ASP.NET Core MVC 使用 EF Core + 原生SQL访问MySql数据库
- EF Core + MySQL数据库插入数据后获取自增列的值
Entity Framework Core简称为 EF Core
2、本教程环境信息
软件/环境 | 说明 |
---|---|
操作系统 | Windows 10 |
SDK | 2.1.401 |
ASP.NET Core | 2.1.3 |
MySQL | 8.0.x |
IDE | Visual Studio Code 1.30 |
浏览器 | Chrome 70 |
VS Code插件 | 版本 | 说明 |
---|---|---|
C# | 1.17.1 | 提供C#智能感知, .NET Core 调试、编译等 |
vscdoe-solution-explorer | 0.3.1 | 提供解决方案视图 |
本篇代码以下代码进行调整:https://github.com/ken-io/asp.net-core-tutorial/tree/master/chapter-02
3、准备工作
安装MySQL数据库:https://dev.mysql.com/downloads/mysql/
- Windows环境
下载安装包后双击安装,一直下一步即可。
- MacOS环境
参考:https://ken.io/note/macos-mysql8-install-config-tutorial
- CentOS环境
参考:https://ken.io/note/centos-mysql8-setup
4、前置知识
- 控制反转(IOC)原则与依赖注入(DI)
ASP.NET Core 默认集成了DI。所有官方模块的引入都要使用DI的方式引入。
https://baike.baidu.com/item/IOC
- Linq使用教程
https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials/working-with-linq
二、EF Core + MySQL 前置准备
EF Core 全称:Entity Framework Core,为微软为 .NET Core平台开发的ORM框架。对应是 .NET Framework平台的 Entity Framework(EF),无论是EF还是EF Core都可以说是 .NET 平台开发效率最高的ORM框架。
1、引入 EF Core + MySQL Provider
EF Core已经集成在 ASP.NET Core 中,但默认并不支持MySQL,如果需要连接MySQL,需要添加MySQL相关的Provider,这里我选择的是:Pomelo.EntityFrameworkCore.MySql
任意命令行操作即可,我用的是VS Code自带的命令行
//进入项目根目录: Ken.Tutorial.Web
cd Ken.Tutorial.Web
//添加Package
dotnet add package Pomelo.EntityFrameworkCore.MySql
这里我添加的 MySql.Data.EntityFrameworkCore 版本是 8.0.13,如果你想跟我使用一样的版本,可以使用以下命令:
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 2.1.4
2、创建MySQL库表
- 创建数据库
CREATE DATABASE ken_tutorial;
- 创建表
USE ken_tutorial; DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`age` int() DEFAULT NULL,
`hobby` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、配置链接字符串
在项目根目录Ken.Tutorial.Web
中创建配置文件appsettings.json
,并写入以下配置
{
"ConnectionStrings": {
"testdb": "server=localhost;database=Ken_Tutorial;uid=root;pwd=root;"
}
}
4、实体&DbContext准备
创建user表对应实体
在项目根目录Ken.Tutorial.Web
中创建目录Models
,并在其中创建类:UserEntity.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Ken.Tutorial.Web.Models
{
[Table("user")]
[Serializable]
public class UserEntity
{
[Key]//主键
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]//自增列
[Column("id")]
public int Id { get; set; } [Column("name")]
public string Name { get; set; } [Column("age")]
public int Age { get; set; } [Column("hobby")]
public string Hobby { get; set; }
}
}
创建DBContext
在项目根目录Ken.Tutorial.Web
中创建目录Repositories
,并在其中创建类:TutorialDbContext.cs
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; namespace Ken.Tutorial.Web.Repositories
{
//宏观上我们可以认为这个类就是数据库本身
public class TutorialDbContext : DbContext
{
private IConfiguration Configuration { get; } public TutorialDbContext(IConfiguration configuration)
{
this.Configuration = configuration;
} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(Configuration.GetConnectionString("testdb"));//需要添加引用:MySQL.Data.EntityFrameworkCore
} ppublic DbSet<UserEntity> Users { get; set; }
}
}
TutorialDbContext 继承了 DbContext,然后再Context初始化时配置MySQL链接。
由于 ASP.NET Core 默认使用了DI组件,所以我们取配置文件,就需要在构造函数中获取
IConfiguration
注入的实例。
在Startup.cs配置注入,以便数据访问类可以访问DI方式获取TutorialDbContext
public void ConfigureServices(IServiceCollection services)
{
//引入MVC模块
services.AddMvc(); //配置DbContext注入
services.AddTransient<TutorialDbContext>();
}
三、EF Core + Linq to entity 访问数据库
1、创建Linq To Entity 数据访问类
using System;
using System.Collections.Generic;
using System.Linq;
using Ken.Tutorial.Web.Models; namespace Ken.Tutorial.Web.Repositories
{
public class TutorialRepository
{
private TutorialDbContext DbContext { get; } public TutorialRepository(TutorialDbContext dbContext)
{
//在构造函数中注入DbContext
this.DbContext = dbContext;
} //添加
public int Add(UserEntity user)
{
using (DbContext)
{
//由于我们在UserEntity.Id配置了自增列的Attribute,EF执行完成后会自动把自增列的值赋值给user.Id
DbContext.Users.Add(user);
return DbContext.SaveChanges();
} } //删除
public int Delete(int id)
{
using (DbContext)
{
var userFromContext = DbContext.Users.FirstOrDefault(u => u.Id == id);
DbContext.Users.Remove(userFromContext);
return DbContext.SaveChanges();
}
} //更新
public int Update(UserEntity user)
{
using (DbContext)
{
var userFromContext = DbContext.Users.FirstOrDefault(u => u.Id == user.Id);
userFromContext.Name = user.Name;
userFromContext.Age = user.Age;
userFromContext.Hobby = user.Hobby;
return DbContext.SaveChanges();
}
} //查询
public UserEntity QueryById(int id)
{
using (DbContext)
{
return DbContext.Users.FirstOrDefault(u => u.Id == id);
}
} //查询集合
public List<UserEntity> QueryByAge(int age)
{
using (DbContext)
{
return DbContext.Users.Where(u => u.Age == age).ToList();
}
} //查看指定列
public List<string> QueryNameByAge(int age)
{
using (DbContext)
{
return DbContext.Users.Where(u => u.Age == age).Select(u => u.Name).ToList();
}
} //分页查询
public List<UserEntity> QueryUserPaging(int pageSize, int page)
{
using (DbContext)
{
return DbContext.Users.Skip(pageSize * (page - )).Take(pageSize).ToList();
}
} //事务:将年龄<0的用户修改年龄为0
public int FixAge()
{
using (DbContext)
{
using (var transaction = DbContext.Database.BeginTransaction())
{
try
{
var userListFromContext = DbContext.Users.Where(u => u.Age < );
foreach (UserEntity u in userListFromContext)
{
u.Age = ;
}
var count = DbContext.SaveChanges();
transaction.Commit();
return count;
}
catch
{
transaction.Rollback();
return ;
}
}
}
}
}
}
2、创建测试API
在Controllers
文件夹下创建EfCoreController.cs
作为访问入口
using System;
using Ken.Tutorial.Web.Models;
using Ken.Tutorial.Web.Repositories;
using Microsoft.AspNetCore.Mvc; namespace Ken.Tutorial.Web.Controllers
{
public class EfCoreController : Controller
{
public TutorialRepository Repository { get; } public EfCoreController(TutorialRepository repository)
{
this.Repository = repository;
} public IActionResult Add(UserEntity user)
{
var message = Repository.Add(user) > ? "success" : "failed";
return Json(new { Message = message, User = user });
} public IActionResult Delete(int id)
{
var message = Repository.Delete(id) > ? "success" : "failed";
return Json(new { Message = message });
} public IActionResult Update(UserEntity user)
{
var message = Repository.Update(user) > ? "success" : "failed";
return Json(new { Message = message, User = user });
} public IActionResult QueryById(int id)
{
var user = Repository.QueryById(id);
return Json(new { User = user });
} public IActionResult QueryByAge(int age)
{
var users = Repository.QueryByAge(age);
return Json(new { Users = users });
} public IActionResult QueryNameByAge(int age)
{
var users = Repository.QueryNameByAge(age);
return Json(new { Users = users });
} public IActionResult QueryUserPaging(int pageSize, int page)
{
var users = Repository.QueryUserPaging(pageSize, page);
return Json(new { Users = users });
} public IActionResult FixAge()
{
var count = Repository.FixAge();
return Json(new { FixCount = count });
} }
}
3、启动测试
- 在Startup.cs中配置Repository注入
public void ConfigureServices(IServiceCollection services)
{
//其他代码省略 //配置Repository注入
services.AddTransient<TutorialRepository>();
}
- 启动项目并测试
API | 示例 |
---|---|
添加用户 | /efcore/add?name=ken&age=18&hobby=coding |
删除用户 | /efcore/delete?id=0 |
更新用户 | /efcore/update?id=1&name=ken&age=666&hobby=codingOrGaming |
查询单个用户 | /efcore/querybyid?id=0 |
查询多个用户 | /efcore/querybyage?age=18 |
查询多个用户名 | /efcore/querynamebyage?age=18 |
分页查询用户 | /efcore/queryuserpaging?pagesize=3&page=1 |
修复异常年龄 | /efcore/fixage |
四、EF Core + 原生SQL 访问数据库
1、创建EF Core + 原生SQL 数据访问类
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Ken.Tutorial.Web.Models;
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient; namespace Ken.Tutorial.Web.Repositories
{
public class TutorialWithSqlRepository
{
private TutorialDbContext DbContext { get; } public TutorialWithSqlRepository(TutorialDbContext dbContext)
{
//在构造函数中注入DbContext
this.DbContext = dbContext;
} //添加
public int Add(UserEntity user)
{
using (var connection = DbContext.Database.GetDbConnection())
{
connection.Open();
var command = connection.CreateCommand() as MySqlCommand;
command.CommandText = "INSERT INTO user (name,age,hobby) VALUES(@name,@age,@hobby)";
command.Parameters.Add(new MySqlParameter()
{
ParameterName = "@name",
DbType = DbType.String,
Value = user.Name
});
command.Parameters.Add(new MySqlParameter()
{
ParameterName = "@age",
DbType = DbType.Int32,
Value = user.Age
});
command.Parameters.Add(new MySqlParameter()
{
ParameterName = "@hobby",
DbType = DbType.String,
Value = user.Hobby
});
var count = command.ExecuteNonQuery();
//获取插入时产生的自增列Id并赋值给user.Id使用
user.Id = (int)command.LastInsertedId;
return count;
} } //删除
public int Delete(int id)
{
using (DbContext)
{
return DbContext.Database.ExecuteSqlCommand(
"DELETE FROM user WHERE id={0}", id);
}
} //更新
public int Update(UserEntity user)
{
using (DbContext)
{
return DbContext.Database.ExecuteSqlCommand(
"UPDATE user SET name={0}, age={1}, hobby={2} WHERE id={3}",
user.Name, user.Age, user.Hobby, user.Id);
}
} //查询
public UserEntity QueryById(int id)
{
using (DbContext)
{
return DbContext.Users.FromSql("SELECT id,name,age,hobby FROM user WHERE id={0}", id).FirstOrDefault();
}
} //查询集合
public List<UserEntity> QueryByAge(int age)
{
using (DbContext)
{
return DbContext.Users.FromSql("SELECT id,name,age,hobby FROM user WHERE age={0}", age).ToList();
}
} //查看指定列
public List<string> QueryNameByAge(int age)
{
using (DbContext)
{
return DbContext.Users.FromSql("SELECT id,name FROM user WHERE age={0}", age).Select(u => u.Name).ToList();
}
} //分页查询
public List<UserEntity> QueryUserPaging(int pageSize, int page)
{
using (DbContext)
{
return DbContext.Users.FromSql("SELECT id,name,age,hobby FROM user LIMIT {0},{1}", pageSize * (page - ), pageSize).ToList();
}
} //事务:将年龄<0的用户修改年龄为0
public int FixAge()
{
using (DbContext)
{
using (var connection = DbContext.Database.GetDbConnection())
{
//打开连接
connection.Open();
//开启事务
using (var transaction = connection.BeginTransaction())
{
try
{
//获取命令对象
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = "UPDATE user SET age=@age WHERE age<@age";
command.Parameters.Add(new MySqlParameter()
{
ParameterName = "@age",
DbType = DbType.Int32,
Value =
});
var count = command.ExecuteNonQuery();
transaction.Commit();
return count;
}
catch (Exception ex)
{
connection.Close();
transaction.Rollback();
return ;
}
} }
}
}
}
}
2、创建测试API
在Controllers
文件夹下创建EfCoreWithSqlController.cs
作为访问入口
using System;
using Ken.Tutorial.Web.Models;
using Ken.Tutorial.Web.Repositories;
using Microsoft.AspNetCore.Mvc; namespace Ken.Tutorial.Web.Controllers
{
public class EfCoreWithSqlController : Controller
{
public TutorialWithSqlRepository Repository { get; } public EfCoreWithSqlController(TutorialWithSqlRepository repository)
{
this.Repository = repository;
} public IActionResult Add(UserEntity user)
{
var message = Repository.Add(user) > ? "success" : "failed";
return Json(new { Message = message, User = user });
} public IActionResult Delete(int id)
{
var message = Repository.Delete(id) > ? "success" : "failed";
return Json(new { Message = message });
} public IActionResult Update(UserEntity user)
{
var message = Repository.Update(user) > ? "success" : "failed";
return Json(new { Message = message, User = user });
} public IActionResult QueryById(int id)
{
var user = Repository.QueryById(id);
return Json(new { User = user });
} public IActionResult QueryByAge(int age)
{
var users = Repository.QueryByAge(age);
return Json(new { Users = users });
} public IActionResult QueryNameByAge(int age)
{
var users = Repository.QueryNameByAge(age);
return Json(new { Users = users });
} public IActionResult QueryUserPaging(int pageSize, int page)
{
var users = Repository.QueryUserPaging(pageSize, page);
return Json(new { Users = users });
} public IActionResult FixAge()
{
var count = Repository.FixAge();
return Json(new { FixCount = count });
} }
}
3、启动测试
- 在Startup.cs中配置Repository注入
public void ConfigureServices(IServiceCollection services)
{
//其他代码省略 //配置Repository注入
services.AddTransient<TutorialWithSqlRepository>();
}
- 启动项目并测试
API | 示例 |
---|---|
添加用户 | /efcorewithsql/add?name=ken&age=18&hobby=coding |
删除用户 | /efcorewithsql/delete?id=0 |
更新用户 | /efcorewithsql/update?id=1&name=ken&age=666&hobby=codingOrGaming |
查询单个用户 | /efcorewithsql/querybyid?id=0 |
查询多个用户 | /efcorewithsql/querybyage?age=18 |
查询多个用户名 | /efcorewithsql/querynamebyage?age=18 |
分页查询用户 | /efcorewithsql/queryuserpaging?pagesize=3&page=1 |
修复异常年龄 | /efcorewithsql/fixage |
本文摘抄自:https://www.cnblogs.com/ken-io/p/aspnet-core-tutorial-entity-framework-core-mysql.html
如有侵权,请告知删除或者修改
ASP.NET Core 入门笔记9,ASP.NET Core + Entity Framework Core 数据访问入门的更多相关文章
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据
Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据
Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio »迁移
Migrations¶ 4 of 4 people found this helpful The Contoso University sample web application demonstra ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组
Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 增、查、改、删操作
Create, Read, Update, and Delete operations¶ 5 of 5 people found this helpful By Tom Dykstra The Con ...
- Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...
- Entity Framework Core(EF Core) 最简单的入门示例
目录 概述 基于 .NET Core 的 EF Core 入门 创建新项目 更改当前目录 安装 Entity Framework Core 创建模型 创建数据库 使用模型 基于 ASP.NET Cor ...
- Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...
- .Net Core 2.0生态(4):Entity Framework Core 2.0 特性介绍和使用指南
前言 这是.Net Core 2.0生态生态介绍的最后一篇,EF一直是我喜欢的一个ORM框架,随着版本升级EF也发展到EF6.x,Entity Framework Core是一个支持跨平台的全新版本, ...
随机推荐
- [USACO19JAN]Redistricting——单调队列优化DP
原题链接 首先有一个\(O(nk)\)的很显然的\(dp\),把荷斯坦牛看成\(1\),把更赛牛看成\(-1\),这样就可以很方便地通过前缀和来判断某一段中谁有优势了 考虑怎么优化,观察转移: \[f ...
- SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚
通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseContro ...
- macOS上更顺手的终端
安装iTerm2.下载地址 https://iterm2.com/downloads/stable/latest 安装Nerd Fonts.下载地址 https://github.com/ryanoa ...
- 字典(dict)
定义 In [4]: dt1 = {'name':'ray','age':18,'height':175} In [5]: dt1 Out[5]: {'name': 'ray', 'age': 18, ...
- maven插件生成可执行jar包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assem ...
- springboot注释层分解图
- SSO 断点登陆
1. 摘要 ( 注意:请仔细看下摘要,留心此文是否是您的菜,若浪费宝贵时间,深感歉意!!!) SSO这一概念由来已久,网络上对应不同场景的成熟SSO解决方案比比皆是,从简单到复杂,各式各样应有尽有!开 ...
- docker下的images 保存和导出
由于迁移测试环境,所以部署的的docker镜像某一些需要迁移到另外一台服务器上面去.. 我是用 docker save -o registry.tar registry 来把相应需要导出的images ...
- Dubbo API 笔记——Dubbo架构与使用
转载于: https://blog.csdn.net/benhuo931115/article/details/78457149 架构演变 单一应用架构 所有功能部署在一个应用上,用于简化增删改查工作 ...
- MongoDB系列二:MongoDB安装过程
一.MongoDB安装,以Linux系统安装为例:(下载:www.mongodb.org 注意使用stable版本) 1.下载最新版本的MongoDB安装包,wget http://fastdl.mo ...