WP.Migration
1.0.2
dotnet add package WP.Migration --version 1.0.2
NuGet\Install-Package WP.Migration -Version 1.0.2
<PackageReference Include="WP.Migration" Version="1.0.2" />
<PackageVersion Include="WP.Migration" Version="1.0.2" />
<PackageReference Include="WP.Migration" />
paket add WP.Migration --version 1.0.2
#r "nuget: WP.Migration, 1.0.2"
#:package WP.Migration@1.0.2
#addin nuget:?package=WP.Migration&version=1.0.2
#tool nuget:?package=WP.Migration&version=1.0.2
概述
主要目的:使用 EF 迁移生成 MySql 数据库时,使生成的数据库表、表包含字段的风格更贴近 mysql。
次要目的:当然也支持 SqlServer、Sqlite风格。
配置示例
{
"MainDB": {
"DBType": 1, // 0 SqlServer、1 MySql、2 Sqlite
"DBName": "Example",
"Connection": "Server=127.0.0.1;User Id=root;Password=123456;Port=3306;Allow User Variables=true;",
"TablePrefix": "CodeWrench123"
}
}
| 配置名称 | 类型 | 描述 |
|---|---|---|
DBType |
Enum | 支持的数据类型(目前支持 0 SqlServer、1 MySql、2 Sqlite)三种。 |
DBName |
String | 数据库的名称 |
Connection |
String | 数据库连接字符串(PS: 这里连接字符串不包括数据库名称配置) |
TablePrefix |
String | 迁移生成的表的前缀 |
使用方式
迁移记录生成配置使用
首先是生成迁移的 **DesignTimeDbContextFactory 不在直接实现 IDesignTimeDbContextFactory 接口,而是继承自 WPDesignTimeDbContextFactory,如:
public class CodeWrenchDesignTimeDbContextFactory : WPDesignTimeDbContextFactory<CodeWrenchDbContext>
{
public CodeWrenchDesignTimeDbContextFactory() : base()
{
}
}
其中WPDesignTimeDbContextFactory构造函数有三个参数,可根据需求配置:
| 参数 | 描述 |
|---|---|
| assemblyName | 在此上下文中维护迁移的程序集的名称,默认为调用程序集的名称 |
| domainName | 该迁移领域名称,默认为assemblyName的顶级域名 |
| migrationsHistoryTable | 记录迁移应用记录的数据库的表的名称 |
这里记录迁移应用记录的数据库的表的名称配置的优先级为:
- 优先使用构造函数传入的
migrationsHistoryTable参数 - 如果没有传入
migrationsHistoryTable参数,则使用"_EFMigrationsHistory_" + 迁移生成的表的前缀/TablePrefix - 如果也没有配置
TablePrefix,则使用"_EFMigrationsHistory_"+ 构造函数传入参数domainName - 最后什么配置都没有则默认为
"_EFMigrationsHistory"
注意
assemblyName、domainName默认值
表字段生成配置使用
先看示例代码:
public class ColumnInfoMap : IEntityTypeConfiguration<ColumnInfo>
{
public void Configure(EntityTypeBuilder<ColumnInfo> builder)
{
builder.ToTableMap(tableBuilder =>
{
tableBuilder.HasComment("列信息");
}).HasKey(x => x.Id);
builder.PropertyMap(x => x.TableInfoId).HasComment("关联表信息Id").IsRequired();
}
}
主要两个方法,先看表名称处理的方法 ToTableMap,使用方式和原生方法 ToTable 使用方式类似,不过增加了一些可选配置参数:
| 参数 | 类型 | 描述 |
|---|---|---|
tablePrefix |
String | 指定的表前缀 |
tableName |
String | 指定的表名称,默认实体名称 |
enableNamespacePrefix |
Boolean | 是否启用命名空间前缀 |
这里表前缀的配置优先级为:
ToTableMap方法指定的参数tablePrefix系统配置文件 appsettings.json 中配置的节点
TablePrefixenableNamespacePrefix为 true 时,调用ToTableMap的方法所在顶级命名空间作为表前缀
然后看 PropertyMap 方法,使用方式同样参照原生方法Property。
Result
如果是 MySQL 数据库,没有配置表前缀名称,同时假设调用程序集的顶级命名空间为 CodeWrench123,使用插件默认配置(即参照上面进行配置):
迁移生成的表名称为:_efmigrationshistory_codewrench123
实体 ColumnInfo 生成的表名称为:codewrench123_column_info
实体 ColumnInfo 的属性 TableInfoId 生成的字段名称为:table_info_id
至此,实现 mysql 风格的 EF 迁移。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Dapper (>= 2.1.4)
- Humanizer.Core (>= 2.14.1)
- Microsoft.EntityFrameworkCore (>= 7.0.10)
- Microsoft.EntityFrameworkCore.Sqlite (>= 7.0.10)
- Microsoft.EntityFrameworkCore.SqlServer (>= 7.0.10)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.1)
- Pomelo.EntityFrameworkCore.MySql (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.