diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xml b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xml new file mode 100644 index 000000000..be68c182b --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xsd b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xsd new file mode 100644 index 000000000..3f3946e28 --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/FodyWeavers.xsd @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName.csproj b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName.csproj new file mode 100644 index 000000000..2307cf26f --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName.csproj @@ -0,0 +1,27 @@ + + + + + + + net8.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.en.md b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.en.md new file mode 100644 index 000000000..114aad0be --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.en.md @@ -0,0 +1,59 @@ +# MySQL Database Migration Guide + +This guide will help you manage MySQL database migrations using the migration scripts. + +## Prerequisites + +1. Ensure .NET Core SDK is installed +2. Ensure Entity Framework Core tools are installed + ```powershell + dotnet tool install --global dotnet-ef + ``` +3. Ensure MySQL connection string is properly configured + +## Usage Instructions + +### 1. Create New Migration + +1. Run the migration script in the `aspnet-core/migrations` directory: + ```powershell + # Use English version + .\MigrateEn.ps1 + + # Or use Chinese version + .\Migrate.ps1 + ``` + +2. Select MySQL database context from the menu: + ``` + [1] PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.MySql + ``` + +3. Enter migration name (optional): + - Press Enter to use default name: `AddNewMigration_yyyyMMdd_HHmmss` + - Or enter custom name, e.g.: `AddNewFeature` + +### 2. Generate SQL Script + +After creating the migration, the script will ask if you want to generate SQL script: + +1. Choose whether to generate SQL script (Y/N) +2. If Y is selected, following options will be available: + - `[A]` - Generate SQL script for all migrations + - `[L]` - Generate SQL script for latest migration only + - `[0-9]` - Generate from specified migration version + +Generated SQL scripts will be saved in: +``` +aspnet-core/InitSql/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.MySql/ +``` + +### 3. Apply Migration + +Generated SQL scripts can be applied to database through: + +1. Using MySQL client tools to execute SQL script directly +2. Or using command line: + ```powershell + mysql -u your_username -p your_database < your_script.sql + ``` diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.md b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.md new file mode 100644 index 000000000..c43698e9c --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/README.md @@ -0,0 +1,59 @@ +# MySQL 数据库迁移指南 + +本指南将帮助您使用迁移脚本来管理 MySQL 数据库的迁移操作。 + +## 前置条件 + +1. 确保已安装 .NET Core SDK +2. 确保已安装 Entity Framework Core 工具 + ```powershell + dotnet tool install --global dotnet-ef + ``` +3. 确保已正确配置 MySQL 连接字符串 + +## 使用说明 + +### 1. 创建新的迁移 + +1. 在 `aspnet-core/migrations` 目录下运行迁移脚本: + ```powershell + # 使用中文版本 + .\Migrate.ps1 + + # 或使用英文版本 + .\MigrateEn.ps1 + ``` + +2. 在菜单中选择 MySQL 数据库上下文: + ``` + [1] PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.MySql + ``` + +3. 输入迁移名称(可选): + - 直接回车将使用默认名称:`AddNewMigration_yyyyMMdd_HHmmss` + - 或输入自定义名称,如:`AddNewFeature` + +### 2. 生成 SQL 脚本 + +在创建迁移后,脚本会询问是否需要生成 SQL 脚本: + +1. 选择是否生成 SQL 脚本 (Y/N) +2. 如果选择 Y,将提供以下选项: + - `[A]` - 生成所有迁移的 SQL 脚本 + - `[L]` - 仅生成最新迁移的 SQL 脚本 + - `[0-9]` - 从指定的迁移版本开始生成 + +生成的 SQL 脚本将保存在: +``` +aspnet-core/InitSql/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.MySql/ +``` + +### 3. 应用迁移 + +生成的 SQL 脚本可以通过以下方式应用到数据库: + +1. 使用 MySQL 客户端工具直接执行 SQL 脚本 +2. 或使用命令行: + ```powershell + mysql -u your_username -p your_database < your_script.sql + ``` diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsDbContextFactory.cs b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsDbContextFactory.cs new file mode 100644 index 000000000..f9adabc2d --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsDbContextFactory.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore; +using PackageName.CompanyName.ProjectName.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; +using System.IO; + +namespace PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName; + +public class SingleMigrationsDbContextFactory : IDesignTimeDbContextFactory +{ + public SingleMigrationsDbContext CreateDbContext(string[] args) + { + var configuration = BuildConfiguration(); + var connectionString = configuration.GetConnectionString("Default"); + DbContextOptionsBuilder builder = null; +#if MySQL + builder = new DbContextOptionsBuilder() + .UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#elif SqlServer + builder = new DbContextOptionsBuilder() + .UseSqlServer(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#elif Sqlite + builder = new DbContextOptionsBuilder() + .UseSqlite(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#elif Oracle + builder = new DbContextOptionsBuilder() + .UseOracle(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#elif OracleDevart + builder = (DbContextOptionsBuilder) new DbContextOptionsBuilder() + .UseOracle(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#elif PostgreSql + builder = new DbContextOptionsBuilder() + .UseNpgsql(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); +#endif + return new SingleMigrationsDbContext(builder!.Options); + } + + private static IConfigurationRoot BuildConfiguration() + { + var builder = new ConfigurationBuilder() + .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), + "../PackageName.CompanyName.ProjectName.AIO.DbMigrator/")) + .AddJsonFile("appsettings.json", optional: false); + + return builder.Build(); + } +} diff --git a/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsEntityFrameworkCoreDatabaseManagementNameModule.cs b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsEntityFrameworkCoreDatabaseManagementNameModule.cs new file mode 100644 index 000000000..6ef5f8205 --- /dev/null +++ b/aspnet-core/templates/aio/content/migrations/PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName/SingleMigrationsEntityFrameworkCoreDatabaseManagementNameModule.cs @@ -0,0 +1,69 @@ +using Microsoft.Extensions.DependencyInjection; +using PackageName.CompanyName.ProjectName.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; +#if MySQL +using Volo.Abp.EntityFrameworkCore.MySQL; +#elif SqlServer +using Volo.Abp.EntityFrameworkCore.SqlServer; +using Microsoft.EntityFrameworkCore.Infrastructure; +#elif Sqlite +using Volo.Abp.EntityFrameworkCore.Sqlite; +#elif Oracle +using Volo.Abp.EntityFrameworkCore.Oracle; +#elif OracleDevart +using Volo.Abp.EntityFrameworkCore.Oracle.Devart; +#elif PostgreSql +using Volo.Abp.EntityFrameworkCore.PostgreSql; +#endif +using Volo.Abp.Modularity; + +namespace PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName; + +[DependsOn( +#if MySQL + typeof(AbpEntityFrameworkCoreMySQLModule), +#elif SqlServer + typeof(AbpEntityFrameworkCoreSqlServerModule), +#elif Sqlite + typeof(AbpEntityFrameworkCoreSqliteModule), +#elif Oracle + typeof(AbpEntityFrameworkCoreOracleModule), +#elif OracleDevart + typeof(AbpEntityFrameworkCoreOracleDevartModule), +#elif PostgreSql + typeof(AbpEntityFrameworkCorePostgreSqlModule), +#endif + typeof(SingleMigrationsEntityFrameworkCoreModule) + )] +public class SingleMigrationsEntityFrameworkCoreDatabaseManagementNameModule : AbpModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddAbpDbContext(); + + Configure(options => + { +#if MySQL + options.UseMySQL(); + options.UseMySQL(); +#elif SqlServer + options.UseSqlServer(); + options.UseSqlServer(builder => + { + // see https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level?view=sql-server-ver16 + // builder.UseCompatibilityLevel(150); + }); +#elif Sqlite + options.UseSqlite(); + options.UseSqlite(); +#elif Oracle || OracleDevart + options.UseOracle(); + options.UseOracle(); +#elif PostgreSql + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);//解决PostgreSql设置为utc时间后无法写入local时区的问题 + options.UseNpgsql(); + options.UseNpgsql(); +#endif + }); + } +}