说在前面的几句话:
这是一个长期的系列,如果我中间没发生其他事情转移我学习目标的话,会一直记录
另外我这个ABP教程将全程使用MySQL,所以第一个文章便是MySQL
dotnet安装请自行百度
我的环境:dotnet 3.1.102 abp 2.2.1 【本地/MySQL 5.7 docker/MySQL 8.0】
一、安装 ABP
dotnet tool install -g Volo.Abp.Cli
二、新建一个工程
abp new Acme.BookStore
三、打开项目(vs2019 或 Rider[我在用])Rider 打开之后可以看到这个目录结构
四、安装Mysql依赖打开NuGet
点击solution->选择图中项目选择EntityFrameworkCore项目
安装 Pomelo.EntityFrameworkCore.MysqlPomelo.EntityFrameworkCore.Mysql.Design
和 Volo.Abp.EntityFrameworkCore.Mysql 三个依赖安装依赖
删除Volo.Abp.EntityFrameworkCore.SqlServer删除SQL server依赖
五、修改配置(将SQL server配置修改为MySQL配置)
第一处修改第一个修改地方
第二处修改第二个修改地方
修改数据库配置(这个是我的配置,MySQL默认端口是3306,我是用docker的MySQL配置)
"Server=localhost;port=8080;Database=Book;uid=root;pwd=123456"数据库配置
六、修改用户实体(不然MySQL建表会报错)
新建类IdentityServerModelCreatingExtensions.cs
using Microsoft.EntityFrameworkCore;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.Clients;
namespace Sun.Book.EntityFrameworkCore
{
public static class IdentityServerModelCreatingExtensions
{
public static void ConfigureIdentityServerForMySQL(this ModelBuilder builder)
{
// Solve the problem of MySQL migration
// https://github.com/abpframework/abp/issues/1920
builder.Entity(b =>
{
// After trying, you can also set it to 400
b.Property(x => x.Value).HasMaxLength(300);
});
builder.Entity(b =>
{
b.Property(x => x.PostLogoutRedirectUri).HasMaxLength(300); // or 400 ?
});
builder.Entity(b =>
{
b.Property(x => x.RedirectUri).HasMaxLength(300); // or 400 ?
});
builder.Entity(b =>
{
b.Property(x => x.Value).HasMaxLength(300); // or 400 ?
});
}
}
}
修改类 BookDbContextModelCreatingExtensions
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.Users;
namespace Sun.Book.EntityFrameworkCore
{
public static class BookDbContextModelCreatingExtensions
{
public static void ConfigureBook(this ModelBuilder builder)
{
Check.NotNull(builder, nameof(builder));
builder.Entity(b =>
{
// After trying, you can also set it to 400
b.Property(x => x.Value).HasMaxLength(300);
});
builder.Entity(b =>
{
b.Property(x => x.PostLogoutRedirectUri).HasMaxLength(300); // or 400 ?
});
builder.Entity(b =>
{
b.Property(x => x.RedirectUri).HasMaxLength(300); // or 400 ?
});
builder.Entity(b =>
{
b.Property(x => x.Value).HasMaxLength(300); // or 400 ?
});
/* Configure your own tables/entities inside here */
//builder.Entity(b =>
//{
// b.ToTable(BookConsts.DbTablePrefix + "YourEntities", BookConsts.DbSchema);
// //...
//});
}
public static void ConfigureCustomUserProperties(this EntityTypeBuilder b)
where TUser: class, IUser
{
//b.Property(nameof(AppUser.MyProperty))...
}
}
}
修改 BookDbContext -> OnModelCreating 方法
builder.ConfigureIdentityServer(options =>
{
options.DatabaseProvider = EfCoreDatabaseProvider.MySql;
});
builder.ConfigureIdentityServerForMySQL();
七、使用 dotnet 命令迁移和更新数据库
需要用到 dotnet ef 命令,参考 https://www.nuget.org/packages/dotnet-ef/
需要先安装这个命令行工具
dotnet tool install --global dotnet-ef --version 3.1.2
迁移和更新数据库
dotnet ef migrations add "init" --project ..\Sun.Book.EntityFrameworkCore.DbMigrations\
dotnet ef database update --project ..\Sun.Book.EntityFrameworkCore.DbMigrations\迁移和更新数据库
执行结束,看一下数据库,数据库连接语句我写的是Book数据库
八、启动项目,并开始继续后面的学习项目启动效果
本文参考文档:
http://hnbc.info/index.php/archives/11/
https://stackoverflow.com/questions/48678495/net-core-2-0-with-mysql-specified-key-was-too-long-max-key-length-is-3072-byt/48739302