Skip to content

Commit d0185cc

Browse files
committed
Add migration support to IDbContext and update Program to initiate migrations
1 parent 11db3aa commit d0185cc

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

Console.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1111
LICENSE = LICENSE
1212
README.md = README.md
1313
README.zh-CN.md = README.zh-CN.md
14+
docker-compose.yaml = docker-compose.yaml
1415
EndProjectSection
1516
EndProject
1617
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console.Core", "src\Console.Core\Console.Core.csproj", "{6C983DDC-6331-4A29-A1B3-0289C9827DE9}"

src/Console.Core/Console.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
1516
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.5">
1617
<PrivateAssets>all</PrivateAssets>
1718
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/Console.Core/ConsoleDbContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public Task SaveChangesAsync()
1717
return base.SaveChangesAsync(CancellationToken.None);
1818
}
1919

20+
public Task BeginMigrationAsync()
21+
{
22+
return Database.MigrateAsync(CancellationToken.None);
23+
}
24+
2025
protected override void OnModelCreating(ModelBuilder modelBuilder)
2126
{
2227
modelBuilder.Entity<PromptHistory>(options =>

src/Console.Core/IDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface IDbContext
1212
public DbSet<UserLike> UserLikes { get; set; }
1313

1414
public Task SaveChangesAsync();
15+
16+
Task BeginMigrationAsync();
1517
}

src/Console.Service/Program.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Console.Core;
12
using Console.Provider.PostgreSQL.Extensions;
23
using Console.Provider.Sqlite.Extensions;
34
using Console.Service.Services;
@@ -36,7 +37,6 @@
3637

3738
var app = builder.Build();
3839

39-
4040
// Configure the HTTP request pipeline.
4141
if (app.Environment.IsDevelopment())
4242
{
@@ -69,4 +69,11 @@ await context.Response.SendFileAsync(
6969
options.Version = "v1";
7070
});
7171

72+
await using (var scope = app.Services.CreateAsyncScope())
73+
{
74+
var dbContext = scope.ServiceProvider.GetRequiredService<IDbContext>();
75+
76+
await dbContext.BeginMigrationAsync();
77+
}
78+
7279
await app.RunAsync();

0 commit comments

Comments
 (0)