public async void It_throws_object_disposed_exception() { var context = new DbContext(new DbContextOptions <DbContext>()); context.Dispose(); // methods (tests all paths) Assert.Throws <ObjectDisposedException>(() => context.Add(new object())); Assert.Throws <ObjectDisposedException>(() => context.Query <object>()); Assert.Throws <ObjectDisposedException>(() => context.Find(typeof(Random), 77)); Assert.Throws <ObjectDisposedException>(() => context.Attach(new object())); Assert.Throws <ObjectDisposedException>(() => context.Update(new object())); Assert.Throws <ObjectDisposedException>(() => context.Remove(new object())); Assert.Throws <ObjectDisposedException>(() => context.SaveChanges()); await Assert.ThrowsAsync <ObjectDisposedException>(() => context.SaveChangesAsync()); await Assert.ThrowsAsync <ObjectDisposedException>(() => context.AddAsync(new object())); await Assert.ThrowsAsync <ObjectDisposedException>(() => context.FindAsync(typeof(Random), 77)); var methodCount = typeof(DbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Count(); var expectedMethodCount = 42; Assert.True( methodCount == expectedMethodCount, userMessage: $"Expected {expectedMethodCount} methods on DbContext but found {methodCount}. " + "Update test to ensure all methods throw ObjectDisposedException after dispose."); // getters Assert.Throws <ObjectDisposedException>(() => context.ChangeTracker); Assert.Throws <ObjectDisposedException>(() => context.Model); var expectedProperties = new List <string> { nameof(DbContext.ChangeTracker), nameof(DbContext.Database), nameof(DbContext.IsDisposed), nameof(DbContext.Model) }; Assert.True( expectedProperties.SequenceEqual( typeof(DbContext) .GetProperties() .Select(p => p.Name) .OrderBy(s => s) .ToList()), userMessage: "Unexpected properties on DbContext. " + "Update test to ensure all getters throw ObjectDisposedException after dispose."); Assert.Throws <ObjectDisposedException>(() => ((IInfrastructure <IServiceProvider>)context).Instance); }
public DbQuery <T> View <T>() where T : class => _dbContext.Query <T>();
public static IQueryable Query(this DbContext context, string entityName) => context.Query(context.Model.FindEntityType(entityName).ClrType);