public async Task SavingChangesAsync_CallsHooks() { // Arrange var hook = new UntypedDbContextHook(); var hooks = new IDbContextHook[] { hook }; var services = new ServiceCollection().AddEntityFrameworkInMemoryDatabase(); var serviceProvider = services.BuildServiceProvider(); var options = new DbContextOptionsBuilder <ExtendedDbContext>() .UseInMemoryDatabase() .UseInternalServiceProvider(serviceProvider) .Options; var context = new TestExtendedDbContext(hooks, options); // Act var product = new Product { }; context.Add(product); await context.SaveChangesAsync(); // Assert Assert.Equal(true, hook.SavingChangesAsyncCalled); Assert.Equal(true, hook.SavedChangesAsyncCalled); }
public void OnModelCreating_CallsHooks() { // Arrange var hook = new UntypedDbContextHook(); var hooks = new IDbContextHook[] { hook }; var services = new ServiceCollection().AddEntityFrameworkInMemoryDatabase(); var serviceProvider = services.BuildServiceProvider(); var options = new DbContextOptionsBuilder <ExtendedDbContext>() .UseInMemoryDatabase() .UseInternalServiceProvider(serviceProvider) .Options; var context = new TestExtendedDbContext(hooks, options); // Act var product = new Product { }; // MA - Services are lazily initiated so we need to perform an operation to set them. context.Add(product); // Assert Assert.Equal(true, hook.OnModelCreatingCalled); }