33
44using System . Data . Common ;
55using Microsoft . EntityFrameworkCore ;
6+ using Microsoft . Extensions . DependencyInjection ;
67
78namespace Microsoft . AspNetCore . Identity . EntityFrameworkCore . InMemory . Test ;
89
910public class InMemoryContext :
1011 InMemoryContext < IdentityUser , IdentityRole , string >
1112{
12- private InMemoryContext ( DbConnection connection ) : base ( connection )
13+ private InMemoryContext ( DbConnection connection , IServiceProvider serviceProvider ) : base ( connection , serviceProvider )
1314 { }
1415
15- public static new InMemoryContext Create ( DbConnection connection )
16- => Initialize ( new InMemoryContext ( connection ) ) ;
16+ public static new InMemoryContext Create ( DbConnection connection , IServiceCollection services = null )
17+ {
18+ services = ConfigureDbServices ( services ) ;
19+ return Initialize ( new InMemoryContext ( connection , services . BuildServiceProvider ( ) ) ) ;
20+ }
21+
22+ public static IServiceCollection ConfigureDbServices ( IServiceCollection services = null )
23+ {
24+ services ??= new ServiceCollection ( ) ;
25+ services . Configure < IdentityOptions > ( options =>
26+ {
27+ options . Stores . SchemaVersion = IdentitySchemaVersions . Version3 ;
28+ } ) ;
29+ return services ;
30+ }
1731
1832 public static TContext Initialize < TContext > ( TContext context ) where TContext : DbContext
1933 {
@@ -28,17 +42,25 @@ public class InMemoryContext<TUser> :
2842 where TUser : IdentityUser
2943{
3044 private readonly DbConnection _connection ;
45+ private readonly IServiceProvider _serviceProvider ;
3146
32- private InMemoryContext ( DbConnection connection )
47+ private InMemoryContext ( DbConnection connection , IServiceProvider serviceProvider )
3348 {
3449 _connection = connection ;
50+ _serviceProvider = serviceProvider ;
3551 }
3652
37- public static InMemoryContext < TUser > Create ( DbConnection connection )
38- => InMemoryContext . Initialize ( new InMemoryContext < TUser > ( connection ) ) ;
53+ public static InMemoryContext < TUser > Create ( DbConnection connection , IServiceCollection services = null )
54+ {
55+ services = InMemoryContext . ConfigureDbServices ( services ) ;
56+ return InMemoryContext . Initialize ( new InMemoryContext < TUser > ( connection , services . BuildServiceProvider ( ) ) ) ;
57+ }
3958
4059 protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
41- => optionsBuilder . UseSqlite ( _connection ) ;
60+ {
61+ optionsBuilder . UseSqlite ( _connection ) ;
62+ optionsBuilder . UseApplicationServiceProvider ( _serviceProvider ) ;
63+ }
4264}
4365
4466public class InMemoryContext < TUser , TRole , TKey > : IdentityDbContext < TUser , TRole , TKey >
@@ -47,17 +69,25 @@ public class InMemoryContext<TUser, TRole, TKey> : IdentityDbContext<TUser, TRol
4769 where TKey : IEquatable < TKey >
4870{
4971 private readonly DbConnection _connection ;
72+ private readonly IServiceProvider _serviceProvider ;
5073
51- protected InMemoryContext ( DbConnection connection )
74+ protected InMemoryContext ( DbConnection connection , IServiceProvider serviceProvider )
5275 {
5376 _connection = connection ;
77+ _serviceProvider = serviceProvider ;
5478 }
5579
56- public static InMemoryContext < TUser , TRole , TKey > Create ( DbConnection connection )
57- => InMemoryContext . Initialize ( new InMemoryContext < TUser , TRole , TKey > ( connection ) ) ;
80+ public static InMemoryContext < TUser , TRole , TKey > Create ( DbConnection connection , IServiceCollection services = null )
81+ {
82+ services = InMemoryContext . ConfigureDbServices ( services ) ;
83+ return InMemoryContext . Initialize ( new InMemoryContext < TUser , TRole , TKey > ( connection , services . BuildServiceProvider ( ) ) ) ;
84+ }
5885
5986 protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
60- => optionsBuilder . UseSqlite ( _connection ) ;
87+ {
88+ optionsBuilder . UseSqlite ( _connection ) ;
89+ optionsBuilder . UseApplicationServiceProvider ( _serviceProvider ) ;
90+ }
6191}
6292
6393public abstract class InMemoryContext < TUser , TRole , TKey , TUserClaim , TUserRole , TUserLogin , TRoleClaim , TUserToken > :
0 commit comments