Skip to content

Commit 00887f3

Browse files
committed
feat: add WarmupDbContext
1 parent 3f77aee commit 00887f3

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 NETCOREXT
3+
Copyright (c) 2025 NETCOREXT
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

src/Netcorext.EntityFramework.UserIdentityPattern.AspNetCore/Extensions/ApplicationBuilderExtension.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,32 @@ public static void MigrateDatabase(this IApplicationBuilder builder)
4747

4848
context.Master.Database.Migrate();
4949
}
50+
public static void WarmupDbContext(this IApplicationBuilder builder)
51+
{
52+
Task.Run(async () =>
53+
{
54+
using var serviceScope = builder.ApplicationServices.CreateScope();
55+
56+
var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContextAdapter>();
57+
58+
try
59+
{
60+
_ = context.Master.Model;
61+
_ = context.Slave.Model;
62+
63+
await context.Master.Database.OpenConnectionAsync();
64+
await context.Slave.Database.OpenConnectionAsync();
65+
66+
await context.Master.Database.ExecuteSqlRawAsync("SELECT 1");
67+
await context.Slave.Database.ExecuteSqlRawAsync("SELECT 1");
68+
69+
await context.Master.Database.CloseConnectionAsync();
70+
await context.Slave.Database.CloseConnectionAsync();
71+
}
72+
catch (Exception ex)
73+
{
74+
Console.Error.WriteLine(ex);
75+
}
76+
});
77+
}
5078
}

0 commit comments

Comments
 (0)