ABP Asp.Net Core 集成 MySql 数据库

.Net技术 码拜 5年前 (2019-07-30) 2982次浏览 0个评论

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

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

操作步骤:

Download Starter Template 下载开始模板

Download the starter template with ASP.NET Core and Entity Framework Core to integrate MySQL. Multi-page template with ASP.NET Core 2.x + .NET Core Framework + Authentication will be explained in this document.

下载 ASP.NET Core 和  Entity Framework Core 的模板用于集成 MySQL。这里使用的是多页面模板 ASP.NET 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 the Official MySQL EF Core Database Provider and the other is Pomelo 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: https://github.com/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 。相关问题的讨论:https://github.com/aspnet/EntityFrameworkCore/issues/10065#issuecomment-336495475

Install 安装

Install the Pomelo.EntityFrameworkCore.MySql NuGet package to the *.EntityFrameworkCore project.

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

Configuration 配置

Configure DbContext 配置 DbContext

Replace YourProjectNameDbContextConfigurer.cs with the following lines

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

public static class MySqlDemoDbContextConfigurer
{
    //CopyRight codebye.com
    public static void Configure(DbContextOptionsBuilder<MySqlDemoDbContext> builder, string connectionString)
    {
        builder.UseMySql(connectionString);
    }
    //CopyRight codebye.com
    public static void Configure(DbContextOptionsBuilder<MySqlDemoDbContext> builder, DbConnection connection)
    {
        builder.UseMySql(connection);
    }
 }

Some configuration and workarounds are needed to use MySQL with ASP.NET 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=codebye.com;uid=root;pwd=codebye.com;database=codebyedb"
  },
  CopyRight codebye.com ...
}

A workaround

To prevent EF Core from calling Program.BuildWebHost() rename BuildWebHost. For example, change it to InitWebHost. To understand why it needs to be renamed, check the following issues:

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

Reason : EF Core 2.0: design-time DbContext discovery changes

Workaround : Design: Allow IDesignTimeDbContextFactory to short-circuit service provider creation

NOTE : If you don’t rename BuildWebHost, you’ll get an error running BuildWebHost method.

Create Database 创建数据库

Remove all migration classes under *.EntityFrameworkCore/Migrations folder. Because Pomelo.EntityFrameworkCore.MySql will add some of its own configurations to work with Entity Framework Core.

Now it’s ready to build the database.

  • Select *.Web.Mvc as the startup project.
  • Open Package Manager Console and select the *.EntityFrameworkCore project.
  • Run the add-migration Initial_Migration command
  • Run the update-database command

删除*.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 Asp.Net Core 集成 MySql 数据库
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!