失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库

abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库

时间:2022-04-10 13:18:32

相关推荐

abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库

Boilerplate(简称ABP)是.Net平台下一个很流行的DDD框架,该框架已经为我们提供了大量的函数,非常方便与搭建企业应用。官方文档:/Pages/Documents

ABP+EF+SQL Server是比较推荐的组合,由于使用的是EF,那么也就意味着我们可以采用其他的数据库,比如MySQL、MariaDB

操作步骤:

Download Starter Template 下载开始模板

Download the starter template CoreandEntity Framework Coreto integrate MySQL.Multi-page template Core 2.x+.NET Core Framework+Authenticationwill be explained in this document.

下载 Core和Entity Framework Core 的模板用于集成 MySQL。这里使用的是多页面模板 Core 2.x+.NET Core Framework

Getting Started 开始

There are two Entity Framework Core providers for MySQL that are mentioned in the Micrososft Docs. One of them is theOfficial MySQL EF Core Database Providerand the other isPomelo EF Core Database Provider for MySQL.

NOTE:The official provider doesn’t support EF Core 2.0 just yet, so the Pomelo EF Core Database Provider will be used in this example, instead.

Related issue: /aspnet/EntityFrameworkCore/issues/10065#issuecomment-336495475

微软文档里提到了两个MySQL的Entity Framework Core类库。一个是官方的MySql.Data.EntityFrameworkCore,另一个是Pomelo EF Core Database Provider for MySQL。

注:由于官方的目前还不支持EF Core 2.0 ,所以本例中使用的是 Pomelo EF Core Database Provider for MySQL 。相关问题的讨论:/aspnet/EntityFrameworkCore/issues/10065#issuecomment-336495475

Install 安装

Install thePomelo.EntityFrameworkCore.MySqlNuGet package to the *.EntityFrameworkCoreproject.

为 *.EntityFrameworkCore 项目 安装NuGet 包 :Pomelo.EntityFrameworkCore.MySql

Configuration 配置

Configure DbContext 配置 DbContext

ReplaceYourProjectNameDbContextConfigurer.cswith the following lines

使用如下代码 替换文件 YourProjectNameDbContextConfigurer.cs 内容

public static class MySqlDemoDbContextConfigurer

{

//CopyRight

public static void Configure(DbContextOptionsBuilder builder, string connectionString)

{

builder.UseMySql(connectionString);

}

//CopyRight

public static void Configure(DbContextOptionsBuilder builder, DbConnection connection)

{

builder.UseMySql(connection);

}

}

Some configuration and workarounds are needed to use MySQL with Core and Entity Framework Core.

一些配置和工作环境 需要使用 MySQL

Configure connection string 配置连接字符串

Change the connection string to your MySQL connection in *.Web.Mvc/appsettings.json. Example:

修改*.Web.Mvc/appsettings.json 文件中的连接字符串,使用MySQL连接字符串。例如:

{

"ConnectionStrings": {

"Default": "server=;uid=root;pwd=;database=codebyedb"

},

CopyRight ...

}

A workaround

To prevent EF Core from callingProgram.BuildWebHost()renameBuildWebHost. For example, change it toInitWebHost. To understand why it needs to be renamed, check the following issues:

为了阻止 EF Core 调用Program.BuildWebHost() ,需要重命名BuildWebHost 。例如,改为InitWebHost 。关于为何需要重命名,可以查看下面的问题讨论:

Create Database 创建数据库

Remove all migration classes under*.EntityFrameworkCore/Migrationsfolder. BecausePomelo.EntityFrameworkCore.MySqlwill add some of its own configurations to work with Entity Framework Core.

Now it’s ready to build the database.

Select*.Web.Mvcas the startup project.

OpenPackage Manager Consoleand select the*.EntityFrameworkCoreproject.

Run theadd-migration Initial_Migrationcommand

Run theupdate-databasecommand

删除*.EntityFrameworkCore/Migrations 文件夹下的所有迁移文件。因为Pomelo.EntityFrameworkCore.MySql 将添加它自己的配置。

现在已经准备好构建数据库。

将*.Web.Mvc 设置为启动项目

打开 程序包管理器控制台 ,选择*.EntityFrameworkCore 项目。

执行 命令add-migration Initial_Migration

运行update-database 命令

The MySQL integration is now complete. You can now run your project with MySQL.

到这里,MySQL集成已经完成。你可以在MySQL环境下运行你的项目了。CodeBye 原创,转载请注明链接出处。谢谢。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ABP Core 集成 MySql 数据库!

如果觉得《abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。