admin管理员组

文章数量:1794759

ASP.NET Core2.1使用EF Core操作MySql数据库及SQL Server数据库迁移MySQL

ASP.NET Core2.1使用EF Core操作MySql数据库及SQL Server数据库迁移MySQL

 

之前一直使用的是SQL serverEF=www.105188/renwen/16861445511256554.html style="color:#2aa0ea" target=_blank class=infotextkey>数据库,近期学习了 core使用EF去操作MySQL,并且进行数据迁移的实现。在网上找了一些教程发现有些坑,现在做一个个人总结。

使用NuGet安装:

Pomelo.EntityFrameworkCore.MySql(2.1.4)和Microsoft.EntityFrameworkCore.Tools(2.1.4)

注:发现高版本会安装失败

软件版本

VS2019

ASP Core:2.1

MySql:8.0

Navicat premium

代码实现链接MySQL

一、项目结构

二、实现过程

1. 创建一个 core 2.1的项目,选择Web应用程序(模型视图控制器)

2. 使用NuGet安装需要的包

(1)Pomelo.EntityFrameworkCore.MySql(2.1.4)

有些较旧的教程里使用的是 MySql.Data.EntityFrameworkCore,但是发现已经弃用,并且执行某些之前的业务逻辑时会出现报错情况,具体没有深究。所以本教程使用的是Pomelo.EntityFrameworkCore.MySql(2.1.4)

(2)Microsoft.EntityFrameworkCore.Tools(2.1.4)

注:发现高版本会安装失败

3. 按照下图的项目结构创建

(1)Data目录并创建相关(文件目录、类、接口)。

(2)创建StudentController控制器及。

(3)在Model创建一个Student实体类。

4.写入相关代码

(1)Student类代码段。

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; namespace NETCoreMysql.Models { [Table("student")] public class Student { [Key] [Column("sno")] public string sno { get; set; } [Column("name")] public string name { get; set; } [Column("age")] public int age { get; set; } } }

(2)在BaseContext上下文类中,BaseContext继承 DbContext 类,通过构造函数注入数据库连接,添加 DbSet<Student> 实体属性。

using Microsoft.EntityFrameworkCore; using netCoreMysql.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace netCoreMysql.Data.Base { public class BaseContext : DbContext { //注入数据库连接 public BaseContext(DbContextOptions<BaseContext> options) : base(options) { } //添加 DbSet<Student> 实体属性 public DbSet<Student> Student { get; set; } } }

(3)在接口IBase中添加 GetStudents()用于查询数据表数据,还可以根据自己的需要增加其他增、删、改操作。

using netCoreMysql.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace netCoreMysql.Data.Interface { public interface IBase { //取全部记录 IEnumerable<Student> GetStudents(); } }

(4)DataBase类继承接口IBase,实现IBase接口里的数据库操作方法,并且在构造函数注入 BaseContext数据库上下文。

using netCoreMysql.Data.Base; using netCoreMysql.Data.Interface; using netCoreMysql.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace netCoreMysql.Data.Implement { public class DataBase:IBase { //数据上下文 public BaseContext Context { get; } public DataBase(BaseContext context) { Context = context; } //取全部记录 public IEnumerable<Student> GetStudents() { return Context.Student.ToList(); } } }

(5)StudentController 控制器中,在构造函数注入 IBase数据库操作,在控制器里调用GetStudents()查询表信。

using Microsoft.AspNetCore.Mvc; using netCoreMysql.Data.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace netCoreMysql.Controllers { public class StudentController : Controller { //注入接口IBase private IBase IBase { get; } public StudentController(IBase iBase) { this.IBase = iBase; } [HttpGet] //取全部记录 public ActionResult<string> Gets() { var stu = "没有记录"; //调用查询方法 var students = IBase.GetStudents(); if (students != null) { stu = ""; foreach (var s in students) { stu += $"学号:{s.sno} \\r\\n"+$"姓名:{s.name} \\r\\n"+$"年龄:{s.age} \\r\\n"; } } return stu; } } }

(6)配置数据库连接串,注册数据库连接,注册数据库操作类。

1.在appsettings.json中加入链接串MysqlConnection

{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "MysqlConnection": "server=localhost;port=3307;database=test;uid=root;pwd=sa123456;CharSet=utf8" } }

2.在Startup.cs 类的 ConfigureServices() 方法中注册数据库链接,以及IBase,DataBase操作类。

public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //注册数据库连接 var connection = Configuration.GetConnectionString("MysqlConnection"); services.AddDbContext<BaseContext>(options => options.UseMySql(connection)); //注册接口操作 services.AddScoped<IBase, DataBase>(); }

5.启动项目操作Gets()方法查询信

项目运行后使用Get请求方式(直接在url输入地址请求)请求StudentController里的Gets()方法,得出以下数据结果,就表示链接MySQL数据库、操作表成功。

SqlServer数据迁移至MySQL

一、打开Navicat premium,链接SqlServer数据库以及创建新的MySQL数据库(两个数据库需要同名),点击导入向导准备迁移。

二、进入导入向导后选择ODBC类型,进行下一步

三、点击...按钮,选择提供程序SqlServer Native client 11.0(有可能版本不一样)

四、连接的服务器名(我使用的是默认名一个点 . ),选择的方式为第一种(第二种不知道为什么测试连接成功但是下一步会失败,知道可以留言),第3那里在下拉列表找到你想要迁移的数据库,进入下一步

五、然后选择你需要的数据表(根据自己的需要可以全选),然后一直下一步就可以了

点击开始按钮进行迁移

迁移完成后就会看到student表已经出现在MySQL的test数据库里

这次的分享就是这样,在对数据库进行迁移操作时要先对数据库进行备份以免出现不可挽回的情况,本文章是一个学习总结。

注:数据库迁移操作前请备份、并谨慎操作

本文标签: 数据库操作EFnetASP