When using InMemory database provider many Entity Framework features doesn’t work. ShamanDbContext implements IInMemoryDatabaseAwareDbProvider interface and has IsUsingInMemoryDatabase property. ShamanDbContext doesn’t set this property by automatically. In order to detect if dbContext uses InMemory db provider, following method can be used in ShamanDbContext deriving class.
1 2 3 4 5 6 |
private static bool DetectInMemory(DbContextOptions options) { var inMemoryOptionsExtensions = options.Extensions .OfType<InMemoryOptionsExtension>(); return inMemoryOptionsExtensions.Any(); } |
The method can be used in constructor
1 2 3 4 |
public MyDbContext(DbContextOptions options) : base(options) { IsUsingInMemoryDatabase = DetectInMemory(options); } |
and OnConfiguring method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); if (optionsBuilder.IsConfigured) { IsUsingInMemoryDatabase = DetectInMemory(optionsBuilder.Options); } else { // ... OTHER STUFF IsUsingInMemoryDatabase = MyOwnDetectionMethod(); } } |