WP.Migration 1.0.2

dotnet add package WP.Migration --version 1.0.2
                    
NuGet\Install-Package WP.Migration -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WP.Migration" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WP.Migration" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="WP.Migration" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WP.Migration --version 1.0.2
                    
#r "nuget: WP.Migration, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WP.Migration@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WP.Migration&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=WP.Migration&version=1.0.2
                    
Install as a Cake Tool

概述

主要目的:使用 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 记录迁移应用记录的数据库的表的名称

这里记录迁移应用记录的数据库的表的名称配置的优先级为:

  1. 优先使用构造函数传入的 migrationsHistoryTable 参数
  2. 如果没有传入migrationsHistoryTable 参数,则使用 "_EFMigrationsHistory_" + 迁移生成的表的前缀/TablePrefix
  3. 如果也没有配置TablePrefix,则使用 "_EFMigrationsHistory_" + 构造函数传入参数 domainName
  4. 最后什么配置都没有则默认为"_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 是否启用命名空间前缀

这里表前缀的配置优先级为:

  1. ToTableMap 方法指定的参数 tablePrefix

  2. 系统配置文件 appsettings.json 中配置的节点 TablePrefix

  3. enableNamespacePrefix为 true 时,调用 ToTableMap 的方法所在顶级命名空间作为表前缀

然后看 PropertyMap 方法,使用方式同样参照原生方法Property。

Result

如果是 MySQL 数据库,没有配置表前缀名称,同时假设调用程序集的顶级命名空间为 CodeWrench123,使用插件默认配置(即参照上面进行配置):

迁移生成的表名称为:_efmigrationshistory_codewrench123

实体 ColumnInfo 生成的表名称为:codewrench123_column_info

实体 ColumnInfo 的属性 TableInfoId 生成的字段名称为:table_info_id

至此,实现 mysql 风格的 EF 迁移。

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 196 2/29/2024
1.0.1 205 12/27/2023
1.0.0 198 12/22/2023