refactor(workflow): 简化设计时数据库上下文工厂配置
- 移除对 Microsoft.Extensions.Configuration 的依赖 - 删除复杂的 appsettings.json 配置文件查找逻辑 - 将连接字符串改为硬编码方式 - 更新数据库端口为 15432 并修改密码为 dev - 精简注释说明 feat(im): 添加 MongoDB Entities 包支持 - 引入 MongoDB.Entities 25.1.0 版本依赖 - 升级 MongoDB.Driver 从 3.4.
This commit is contained in:
parent
d1d9eb4680
commit
f8677deeee
@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.7" />
|
||||
<PackageReference Include="MongoDB.Entities" Version="25.1.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7">
|
||||
@ -14,7 +15,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.8.31" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.8.1" />
|
||||
<PackageReference Include="Volo.Abp.Core" Version="10.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@ -1,39 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Workflow.Infrastructure.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// EF Core 设计时工厂,供 dotnet ef migrations 命令使用。
|
||||
/// 关键:必须启用 UseSnakeCaseNamingConvention(与运行时一致),否则迁移会用 PascalCase 列名。
|
||||
/// </summary>
|
||||
/// <summary>EF Core 设计时工厂。硬编码连接串,不依赖 appsettings 路径。</summary>
|
||||
public class WorkflowDbContextDesignTimeFactory : IDesignTimeDbContextFactory<WorkflowDbContext>
|
||||
{
|
||||
public WorkflowDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
// 向上查找 Api 项目的 appsettings.json(兼容不同工作目录)
|
||||
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "Workflow.Api");
|
||||
if (!Directory.Exists(basePath))
|
||||
{
|
||||
// 如果从 Infrastructure 目录运行,尝试另一种路径
|
||||
basePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "Api");
|
||||
}
|
||||
|
||||
var config = new ConfigurationBuilder()
|
||||
.SetBasePath(basePath)
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.Build();
|
||||
|
||||
var connectionString = config.GetConnectionString("Default")
|
||||
?? config.GetConnectionString("Workflow")
|
||||
?? "Host=localhost;Port=5432;Database=workflow;Username=rag;Password=REDACTED_DEV_PASSWORD";
|
||||
|
||||
var options = new DbContextOptionsBuilder<WorkflowDbContext>()
|
||||
.UseNpgsql(connectionString)
|
||||
.UseNpgsql("Host=localhost;Port=15432;Database=workflow;Username=rag;Password=dev")
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.Options;
|
||||
|
||||
return new WorkflowDbContext(options);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user