7 changed files with 295 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
||||
|
<ConfigureAwait /> |
||||
|
</Weavers> |
||||
@ -0,0 +1,30 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
||||
|
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
||||
|
<xs:element name="Weavers"> |
||||
|
<xs:complexType> |
||||
|
<xs:all> |
||||
|
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
||||
|
<xs:complexType> |
||||
|
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:all> |
||||
|
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
||||
|
<xs:annotation> |
||||
|
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
||||
|
</xs:annotation> |
||||
|
</xs:attribute> |
||||
|
</xs:complexType> |
||||
|
</xs:element> |
||||
|
</xs:schema> |
||||
@ -0,0 +1,27 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="../../common.props" /> |
||||
|
<Import Project="../../configureawait.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net8.0</TargetFramework> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools"> |
||||
|
<PrivateAssets>all</PrivateAssets> |
||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
||||
|
</PackageReference> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.MySql" Condition="'$(MySQL)'=='true'" /> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.SqlServer" Condition="'$(SqlServer)'=='true'" /> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Condition="'$(Sqlite)'=='true'" /> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Oracle" Condition="'$(Oracle)'=='true'" /> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Oracle.Devart" Condition="'$(OracleDevart)'=='true'" /> |
||||
|
<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Condition="'$(PostgreSql)'=='true'" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore\PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -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 |
||||
|
``` |
||||
@ -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 |
||||
|
``` |
||||
@ -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<SingleMigrationsDbContext> |
||||
|
{ |
||||
|
public SingleMigrationsDbContext CreateDbContext(string[] args) |
||||
|
{ |
||||
|
var configuration = BuildConfiguration(); |
||||
|
var connectionString = configuration.GetConnectionString("Default"); |
||||
|
DbContextOptionsBuilder<SingleMigrationsDbContext> builder = null; |
||||
|
#if MySQL
|
||||
|
builder = new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); |
||||
|
#elif SqlServer
|
||||
|
builder = new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.UseSqlServer(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); |
||||
|
#elif Sqlite
|
||||
|
builder = new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.UseSqlite(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); |
||||
|
#elif Oracle
|
||||
|
builder = new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.UseOracle(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); |
||||
|
#elif OracleDevart
|
||||
|
builder = (DbContextOptionsBuilder<SingleMigrationsDbContext>) new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.UseOracle(connectionString, b => b.MigrationsAssembly("PackageName.CompanyName.ProjectName.AIO.EntityFrameworkCore.DatabaseManagementName")); |
||||
|
#elif PostgreSql
|
||||
|
builder = new DbContextOptionsBuilder<SingleMigrationsDbContext>() |
||||
|
.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(); |
||||
|
} |
||||
|
} |
||||
@ -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<SingleMigrationsDbContext>(); |
||||
|
|
||||
|
Configure<AbpDbContextOptions>(options => |
||||
|
{ |
||||
|
#if MySQL
|
||||
|
options.UseMySQL(); |
||||
|
options.UseMySQL<ProjectNameDbContext>(); |
||||
|
#elif SqlServer
|
||||
|
options.UseSqlServer(); |
||||
|
options.UseSqlServer<ProjectNameDbContext>(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<ProjectNameDbContext>(); |
||||
|
#elif Oracle || OracleDevart
|
||||
|
options.UseOracle(); |
||||
|
options.UseOracle<ProjectNameDbContext>(); |
||||
|
#elif PostgreSql
|
||||
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);//解决PostgreSql设置为utc时间后无法写入local时区的问题
|
||||
|
options.UseNpgsql(); |
||||
|
options.UseNpgsql<ProjectNameDbContext>(); |
||||
|
#endif
|
||||
|
}); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue