forked from udap-tools/udap-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseProviderBuilder.cs
74 lines (59 loc) · 2.36 KB
/
DataBaseProviderBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#region (c) 2023 Joseph Shook. All rights reserved.
// /*
// Authors:
// Joseph Shook [email protected]
//
// See LICENSE in the project root for license information.
// */
#endregion
//
// This code was inspired from Duende Identity Server Tests
//
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace UdapServer.Tests;
/// <summary>
/// Pick you database provider
/// </summary>
public class DatabaseProviderBuilder
{
public static DbContextOptions<TDbContext> BuildInMemory<TDbContext, TStoreOptions>(string name,
TStoreOptions storeOptions)
where TDbContext : DbContext
where TStoreOptions : class
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(storeOptions);
var builder = new DbContextOptionsBuilder<TDbContext>();
builder.UseInMemoryDatabase(name);
builder.UseApplicationServiceProvider(serviceCollection.BuildServiceProvider());
return builder.Options;
}
public static DbContextOptions<TDbContext> BuildSqlite<TDbContext, TStoreOptions>(string name,
TStoreOptions storeOptions)
where TDbContext : DbContext
where TStoreOptions : class
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(storeOptions);
var builder = new DbContextOptionsBuilder<TDbContext>();
builder.UseSqlite($"Data Source=Udap.Idp.db.{name}.db");
builder.UseApplicationServiceProvider(serviceCollection.BuildServiceProvider());
return builder.Options;
}
public static DbContextOptions<TDbContext> BuildLocalDb<TDbContext, TStoreOptions>(string name,
TStoreOptions storeOptions)
where TDbContext : DbContext
where TStoreOptions : class
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(storeOptions);
var builder = new DbContextOptionsBuilder<TDbContext>();
builder.UseSqlServer(
$@"Data Source=(LocalDb)\MSSQLLocalDB;database=Udap.Idp.db.{name};trusted_connection=yes;");
builder.UseApplicationServiceProvider(serviceCollection.BuildServiceProvider());
return builder.Options;
}
}