一:開發環境
- 開發工具:Vs2019 16.6.0
- 運行時:. Net Core 3.1
- 數據庫:MySql
- abp版本:2.7.0
二:構建項目
1、安裝abp CLT
dotnet tool install -g Volo.Abp.Cli
2、如果之前安裝過,則更新
dotnet tool update -g Volo.Abp.Cli
3、使用abp官方CLI工具創建項目
abp new Dnc.Abp
三:切換Mysql
目前只支持構建mongodb、SqlServer數據庫,需要手動切換Mysql數據庫
1、使用 Vs 2019 16.6.0 打開項目,進行Nuget還原
2、修改數據庫連接字符串
在Dnc.Abp.Web項目中 appsettings.json 文件修改數據庫連接字符串
"ConnectionStrings": { "Default": "Server=localhost;Database=test;Uid=wuzhd;Pwd=xxxxxx" },
3、管理“Dnc.Abp.EntityFrameworkCore”項目中Nuget程序包,卸載其中的“Volo.Abp.EntityFrameworkCore.SqlServer”包,並瀏覽安裝“Volo.Abp.EntityFrameworkCore.MySQL”程序包。如下圖
4、切換Mysql
4.1、在 Dnc.Abp.EntityFrameworkCore 項目下 AbpEntityFrameworkCoreModule.cs 文件中修改 Configure 為 options.UseMySQL();
4.2、將依賴項目 typeof(AbpEntityFrameworkCoreSqlServerModule) 修改為 typeof(AbpEntityFrameworkCoreMySQLModule)
4.3、在 Dnc.Abp.EntityFrameworkCore.DbMigrations 項目下 AbpMigrationsDbContextFactory.cs 文件中修改為.UseMySql
var builder = new DbContextOptionsBuilder<AbpMigrationsDbContext>() .UseMySql(configuration.GetConnectionString("Default"));
4.4、在 Dnc.Abp.EntityFrameworkCore 項目下 OnModelCreating 方法中增加如下配置
builder.ConfigureIdentityServer(options => { options.DatabaseProvider = EfCoreDatabaseProvider.MySql; });
5、生成遷移
至此,Mysql配置的修改基本完成,項目也沒有錯誤提示了,將Dnc.Abp.Web項目設置為啟動項目,在程序包管理控制台,將默認項目設置為“Dnc.Abp.EntityFrameworkCore.DbMigrations”,輸入add-migration
命令重新生成遷移。
M> add-migration 位於命令管道位置 1 的 cmdlet Add-Migration 請為以下參數提供值: Name: init Build started... Build succeeded. To undo this action, use Remove-Migration. PM>
如上,可以正常生成遷移文件。但是如果在執行update-database
時,提示如下錯誤:
Failed executing DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLEIdentityServerApiSecrets
(`Type` varchar(250) CHARACTER SET utf8mb4 NOT NULL, `Value` longtext CHARACTER SET utf8mb4 NOT NULL, `ApiResourceId` char(36) NOT NULL, `Description` varchar(2000) CHARACTER SET utf8mb4 NULL, `Expiration` datetime(6) NULL, CONSTRAINT `PK_IdentityServerApiSecrets` PRIMARY KEY (`ApiResourceId`, `Type`, `Value`), CONSTRAINT `FK_IdentityServerApiSecrets_IdentityServerApiResources_ApiResou~` FOREIGN KEY (`ApiResourceId`) REFERENCES `IdentityServerApiResources` (`Id`) ON DELETE CASCADE
);
.....
BLOB/TEXT column 'Value' used in key specification without a key length
解決方法為;在 Dnc.Abp.EntityFrameworkCore 項目OnModelCreating方法中增加如下配置:
builder.ConfigureIdentityServer(options => { options.DatabaseProvider = EfCoreDatabaseProvider.MySql; });
全部修改完成后,刪除Migration文件夾,重新執行add-migration
生成遷移,並執行update-database
命令,同步數據庫。成功!