abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库
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 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.
一些配置和工作環境 需要使用 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 。關于為何需要重命名,可以查看下面的問題討論:
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 數據庫!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的abp mysql .net core_ABP Asp.Net Core 集成 MySql 数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 命php令删除文件夹,window_wi
- 下一篇: 量子计算机功率,量子计算功耗知多少