abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库
ASP.NET Boilerplate(簡(jiǎn)稱ABP)是.Net平臺(tái)下一個(gè)很流行的DDD框架,該框架已經(jīng)為我們提供了大量的函數(shù),非常方便與搭建企業(yè)應(yīng)用。官方文檔:http://www.aspnetboilerplate.com/Pages/Documents
ABP+EF+SQL Server是比較推薦的組合,由于使用的是EF,那么也就意味著我們可以采用其他的數(shù)據(jù)庫,比如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
微軟文檔里提到了兩個(gè)MySQL的Entity Framework Core類庫。一個(gè)是官方的MySql.Data.EntityFrameworkCore,另一個(gè)是Pomelo EF Core Database Provider for MySQL。
注:由于官方的目前還不支持EF Core 2.0 ,所以本例中使用的是 Pomelo EF Core Database Provider for MySQL 。相關(guān)問題的討論:https://github.com/aspnet/EntityFrameworkCore/issues/10065#issuecomment-336495475
Install 安裝
Install the?Pomelo.EntityFrameworkCore.MySql?NuGet package to the *.EntityFrameworkCore?project.
為 *.EntityFrameworkCore 項(xiàng)目 安裝?NuGet 包 :?Pomelo.EntityFrameworkCore.MySql
Configuration 配置
Configure DbContext 配置 DbContext
Replace?YourProjectNameDbContextConfigurer.cs?with the following lines
使用如下代碼 替換文件 YourProjectNameDbContextConfigurer.cs 內(nèi)容
public static class MySqlDemoDbContextConfigurer
{
//CopyRight codebye.com
public static void Configure(DbContextOptionsBuilder builder, string connectionString)
{
builder.UseMySql(connectionString);
}
//CopyRight codebye.com
public static void Configure(DbContextOptionsBuilder builder, DbConnection connection)
{
builder.UseMySql(connection);
}
}
Some configuration and workarounds are needed to use MySQL with ASP.NET Core and Entity Framework Core.
一些配置和工作環(huán)境 需要使用 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 調(diào)用?Program.BuildWebHost() ,需要重命名?BuildWebHost 。例如,改為?InitWebHost 。關(guān)于為何需要重命名,可以查看下面的問題討論:
Create Database 創(chuàng)建數(shù)據(jù)庫
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 文件夾下的所有遷移文件。因?yàn)??Pomelo.EntityFrameworkCore.MySql 將添加它自己的配置。
現(xiàn)在已經(jīng)準(zhǔn)備好構(gòu)建數(shù)據(jù)庫。
將??*.Web.Mvc? 設(shè)置為啟動(dòng)項(xiàng)目
打開 程序包管理器控制臺(tái) ,選擇?*.EntityFrameworkCore 項(xiàng)目。
執(zhí)行 命令?add-migration Initial_Migration
運(yùn)行??update-database 命令
The MySQL integration is now complete. You can now run your project with MySQL.
到這里,MySQL集成已經(jīng)完成。你可以在MySQL環(huán)境下運(yùn)行你的項(xiàng)目了。CodeBye 原創(chuàng),轉(zhuǎn)載請(qǐng)注明鏈接出處。謝謝。
CodeBye 版權(quán)所有丨如未注明 , 均為原創(chuàng)丨本網(wǎng)站采用BY-NC-SA協(xié)議進(jìn)行授權(quán) , 轉(zhuǎn)載請(qǐng)注明ABP Asp.Net Core 集成 MySql 數(shù)據(jù)庫!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 命php令删除文件夹,window_wi
- 下一篇: linux cmake编译源码,linu