Skip to content

Commit 2b0eadd

Browse files
committed
Refactor database context to support both PostgreSQL and SQLite, update connection strings, and add IDbContext interface
1 parent 4b75fd3 commit 2b0eadd

28 files changed

+571
-108
lines changed

Console.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,42 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
README.zh-CN.md = README.zh-CN.md
1515
EndProjectSection
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console.Core", "src\Console.Core\Console.Core.csproj", "{6C983DDC-6331-4A29-A1B3-0289C9827DE9}"
18+
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Provider", "Provider", "{9B04F068-ED71-46A3-AB3B-75DE12CE6A4D}"
20+
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console.Provider.Sqlite", "src\Provider\Console.Provider.Sqlite\Console.Provider.Sqlite.csproj", "{C3313AB9-0966-42A0-B826-E66EF362F87D}"
22+
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console.Provider.PostgreSQL", "src\Provider\Console.Provider.PostgreSQL\Console.Provider.PostgreSQL.csproj", "{E3B965E6-F9DA-437F-933E-185D6E0C8CA0}"
24+
EndProject
1725
Global
1826
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1927
Debug|Any CPU = Debug|Any CPU
2028
Release|Any CPU = Release|Any CPU
2129
EndGlobalSection
2230
GlobalSection(NestedProjects) = preSolution
2331
{FF42A437-883E-4F9A-931E-B453A918CBDD} = {9DAEB6D5-AA9D-45AA-A85E-C79C3768CC7E}
32+
{6C983DDC-6331-4A29-A1B3-0289C9827DE9} = {9DAEB6D5-AA9D-45AA-A85E-C79C3768CC7E}
33+
{9B04F068-ED71-46A3-AB3B-75DE12CE6A4D} = {9DAEB6D5-AA9D-45AA-A85E-C79C3768CC7E}
34+
{C3313AB9-0966-42A0-B826-E66EF362F87D} = {9B04F068-ED71-46A3-AB3B-75DE12CE6A4D}
35+
{E3B965E6-F9DA-437F-933E-185D6E0C8CA0} = {9B04F068-ED71-46A3-AB3B-75DE12CE6A4D}
2436
EndGlobalSection
2537
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2638
{FF42A437-883E-4F9A-931E-B453A918CBDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2739
{FF42A437-883E-4F9A-931E-B453A918CBDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
2840
{FF42A437-883E-4F9A-931E-B453A918CBDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
2941
{FF42A437-883E-4F9A-931E-B453A918CBDD}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{6C983DDC-6331-4A29-A1B3-0289C9827DE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{6C983DDC-6331-4A29-A1B3-0289C9827DE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{6C983DDC-6331-4A29-A1B3-0289C9827DE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{6C983DDC-6331-4A29-A1B3-0289C9827DE9}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{C3313AB9-0966-42A0-B826-E66EF362F87D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{C3313AB9-0966-42A0-B826-E66EF362F87D}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{C3313AB9-0966-42A0-B826-E66EF362F87D}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{C3313AB9-0966-42A0-B826-E66EF362F87D}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{E3B965E6-F9DA-437F-933E-185D6E0C8CA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{E3B965E6-F9DA-437F-933E-185D6E0C8CA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{E3B965E6-F9DA-437F-933E-185D6E0C8CA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{E3B965E6-F9DA-437F-933E-185D6E0C8CA0}.Release|Any CPU.Build.0 = Release|Any CPU
3054
EndGlobalSection
3155
EndGlobal

docker-compose.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
image: registry.cn-shenzhen.aliyuncs.com/tokengo/console
44
ports:
55
- 10426:8080
6-
build:
7-
context: .
8-
dockerfile: src/Console.Service/Dockerfile
9-
6+
environment:
7+
- TZ=Asia/Shanghai
8+
volumes:
9+
- ./data:/data

src/Console.Core/Console.Core.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.5">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
</ItemGroup>
20+
21+
</Project>

src/Console.Service/DbAccess/ConsoleDbContext.cs renamed to src/Console.Core/ConsoleDbContext.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
using Console.Service.Entities;
22
using Microsoft.EntityFrameworkCore;
33

4-
namespace Console.Service.DbAccess;
4+
namespace Console.Core;
55

6-
public class ConsoleDbContext(DbContextOptions<ConsoleDbContext> options) : DbContext(options)
6+
public class ConsoleDbContext<TDbContext>(DbContextOptions<TDbContext> options)
7+
: DbContext(options), IDbContext where TDbContext : DbContext
78
{
89
public DbSet<PromptHistory> PromptHistory { get; set; } = null!;
9-
10+
1011
public DbSet<PromptTemplate> PromptTemplates { get; set; } = null!;
1112

1213
public DbSet<UserLike> UserLikes { get; set; } = null!;
1314

15+
public Task SaveChangesAsync()
16+
{
17+
return base.SaveChangesAsync(CancellationToken.None);
18+
}
19+
1420
protected override void OnModelCreating(ModelBuilder modelBuilder)
1521
{
1622
modelBuilder.Entity<PromptHistory>(options =>
@@ -39,8 +45,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3945
options.Property(x => x.Content)
4046
.IsRequired();
4147

42-
options.Property(x => x.Tags)
43-
.HasDefaultValue("[]");
48+
options.Property(x => x.Tags);
4449

4550
options.Property(x => x.UserId)
4651
.HasMaxLength(100);

src/Console.Core/IDbContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Console.Service.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace Console.Core;
5+
6+
public interface IDbContext
7+
{
8+
public DbSet<PromptHistory> PromptHistory { get; set; }
9+
10+
public DbSet<PromptTemplate> PromptTemplates { get; set; }
11+
12+
public DbSet<UserLike> UserLikes { get; set; }
13+
14+
public Task SaveChangesAsync();
15+
}

src/Console.Service/Console.Service.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
<PackageReference Include="FastService" Version="0.1.3" />
1313
<PackageReference Include="FastService.Analyzers" Version="0.1.3" />
1414
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
1615
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
1716
<PrivateAssets>all</PrivateAssets>
1817
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1918
</PackageReference>
20-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.5">
21-
<PrivateAssets>all</PrivateAssets>
22-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23-
</PackageReference>
2419
<PackageReference Include="Microsoft.SemanticKernel" Version="1.54.0" />
2520
<PackageReference Include="Microsoft.SemanticKernel.Agents.Core" Version="1.54.0" />
2621
<PackageReference Include="Microsoft.SemanticKernel.Agents.OpenAI" Version="1.54.0-preview" />
@@ -51,6 +46,7 @@
5146
</ItemGroup>
5247

5348
<ItemGroup>
49+
<Folder Include="DbAccess\" />
5450
<Folder Include="Infrastructure\" />
5551
</ItemGroup>
5652

@@ -69,4 +65,10 @@
6965
</Content>
7066
</ItemGroup>
7167

68+
<ItemGroup>
69+
<ProjectReference Include="..\Console.Core\Console.Core.csproj" />
70+
<ProjectReference Include="..\Provider\Console.Provider.PostgreSQL\Console.Provider.PostgreSQL.csproj" />
71+
<ProjectReference Include="..\Provider\Console.Provider.Sqlite\Console.Provider.Sqlite.csproj" />
72+
</ItemGroup>
73+
7274
</Project>

src/Console.Service/Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
2-
USER root
2+
USER $APP_UID
33
WORKDIR /app
44
EXPOSE 8080
55
EXPOSE 8081
@@ -15,14 +15,18 @@ RUN yarn
1515
RUN npm i --force
1616
RUN yarn run build
1717

18+
1819
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
1920
ARG BUILD_CONFIGURATION=Release
2021
WORKDIR /src
22+
COPY ["src/Console.Service/Console.Service.csproj", "src/Console.Service/"]
23+
COPY ["src/Provider/Console.Provider.Sqlite/Console.Provider.Sqlite.csproj", "src/Provider/Console.Provider.Sqlite/"]
24+
COPY ["src/Console.Core/Console.Core.csproj", "src/Console.Core/"]
25+
COPY ["src/Provider/Console.Provider.PostgreSQL/Console.Provider.PostgreSQL.csproj", "src/Provider/Console.Provider.PostgreSQL/"]
26+
RUN dotnet restore "src/Console.Service/Console.Service.csproj"
2127
# 设置时区
2228
ENV TZ=Asia/Shanghai
2329
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
24-
COPY ["src/Console.Service/Console.Service.csproj", "src/Console.Service/"]
25-
RUN dotnet restore "src/Console.Service/Console.Service.csproj"
2630
COPY . .
2731
WORKDIR "/src/src/Console.Service"
2832
RUN dotnet build "./Console.Service.csproj" -c $BUILD_CONFIGURATION -o /app/build
@@ -36,4 +40,3 @@ WORKDIR /app
3640
COPY --from=publish /app/publish .
3741
COPY --from=web /src/dist ./wwwroot
3842
ENTRYPOINT ["dotnet", "Console.Service.dll"]
39-

0 commit comments

Comments
 (0)