public void RemoveEntity_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 { Id = 1 }; context.Remove(product); // Assert Assert.Equal(true, hook.RemovingEntryCalled); Assert.Equal(true, hook.RemovedEntryCalled); }
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); }
public void DbContextType_AppliedToHooks() { // Arrange var untypedHook = new UntypedDbContextHook(); var hooks = new IDbContextHook[] { untypedHook }; var services = new ServiceCollection().AddEntityFrameworkInMemoryDatabase(); var serviceProvider = services.BuildServiceProvider(); var options = new DbContextOptionsBuilder <ExtendedDbContext>() .UseInMemoryDatabase() .UseInternalServiceProvider(serviceProvider) .Options; // Act var context = new TestExtendedDbContext(hooks, options); // Assert Assert.Equal(typeof(TestExtendedDbContext), untypedHook.DbContextType); }
public void Supports_UntypedHooks() { // Arrange var untypedHook = new UntypedDbContextHook(); var hooks = new IDbContextHook[] { untypedHook }; 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 // MA - Services are lazily initiated so we need to perform an operation to set them. context.SaveChanges(); // Assert Assert.Equal(true, untypedHook.OnConfiguringCalled); }