项目里配置mysql_001 ABP 新建项目及配置MySQL

说在前面的几句话:

这是一个长期的系列,如果我中间没发生其他事情转移我学习目标的话,会一直记录

另外我这个ABP教程将全程使用MySQL,所以第一个文章便是MySQL

dotnet安装请自行百度

我的环境:dotnet 3.1.102    abp 2.2.1   【本地/MySQL 5.7   docker/MySQL 8.0】a523adce009417286c0349f2fb00a754.png

一、安装 ABP

dotnet tool install -g Volo.Abp.Cli

二、新建一个工程

abp new Acme.BookStore

三、打开项目(vs2019 或 Rider[我在用])16ffde9f8d603b246d162eb262e25ef2.pngRider 打开之后可以看到这个目录结构

四、安装Mysql依赖ad8a6d128680b03f8ac2e36841731d0a.png打开NuGet

点击solution->选择图中项目b7610b0e0ee1f2ad04b332ba20b719e6.png选择EntityFrameworkCore项目

安装 Pomelo.EntityFrameworkCore.MysqlPomelo.EntityFrameworkCore.Mysql.Design

和 Volo.Abp.EntityFrameworkCore.Mysql 三个依赖df7681bcc9b62392675efaa3cd84c56e.png安装依赖

删除Volo.Abp.EntityFrameworkCore.SqlServer0a4dbb78bd5fac962eca2ed10f5fa728.png删除SQL server依赖

五、修改配置(将SQL server配置修改为MySQL配置)

第一处修改def263597fd1a85411cd1b00c5167993.png第一个修改地方

第二处修改e2ffe26f0e6b9e29290252f424e0a2a4.png第二个修改地方

修改数据库配置(这个是我的配置,MySQL默认端口是3306,我是用docker的MySQL配置)

"Server=localhost;port=8080;Database=Book;uid=root;pwd=123456"c6cafeee7c3ab5e362719f87aebac24f.png数据库配置

六、修改用户实体(不然MySQL建表会报错)

新建类IdentityServerModelCreatingExtensions.cs55b72d1b242b5ae1621daf2cf1c35ff6.png

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 ?

});

}

}

}

修改类 BookDbContextModelCreatingExtensions4bdd2fb67606b8ba67d90b07fe57649f.png

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 方法90d26b5409977f88b8a9bdfe458f3372.png

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\eefcdb3f7b197f9684ae7bc8c860949e.png迁移和更新数据库

执行结束,看一下数据库,数据库连接语句我写的是Book09fda43f99f702682783ab59003c2ace.png数据库

八、启动项目,并开始继续后面的学习eef5156f87332a452f6fe1eeb354b7ef.png项目启动效果

本文参考文档:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值