本文整理汇总了C#中DbContext.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# DbContext.Dispose方法的具体用法?C# DbContext.Dispose怎么用?C# DbContext.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbContext
的用法示例。
在下文中一共展示了DbContext.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnitTestDatabaseContext
public UnitTestDatabaseContext(DbContext dbContext)
{
if (_dbContext == null)
{
_dbContext = dbContext;
}
else
{
dbContext.Dispose();
}
}
示例2: EndDbContext
private void EndDbContext(DbContext dbContext)
{
if (dbContext != null)
{
if (HttpContext.Current != null)
{
if (HttpContext.Current.Error == null)
{
dbContext.SaveChanges();
}
}
else
{
dbContext.SaveChanges();
}
dbContext.Database.Connection.Close();
dbContext.Database.Connection.Dispose();
dbContext.Dispose();
}
}
示例3: It_disposes_scope
public void It_disposes_scope()
{
var fakeServiceProvider = new FakeServiceProvider();
var context = new DbContext(fakeServiceProvider, new DbContextOptions<DbContext>());
var scopeService = Assert.IsType<FakeServiceProvider.FakeServiceScope>(context.GetService<IServiceScopeFactory>().CreateScope());
Assert.False(scopeService.Disposed);
context.Dispose();
Assert.True(scopeService.Disposed);
Assert.Throws<ObjectDisposedException>(() => ((IInfrastructure<IServiceProvider>)context).Instance);
}
示例4: It_throws_object_disposed_exception
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.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());
var methodCount = typeof(DbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Count();
var expectedMethodCount = 27;
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> { "ChangeTracker", "Database", "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);
}
示例5: CloseContext
public void CloseContext(DbContext context)
{
context.Dispose();
}
示例6: FakeStudentRepository
public FakeStudentRepository(DbContext dbContext)
: base(dbContext)
{
dbContext.Dispose();
students = new List<Student>();
}
示例7: Services_wire_up_correctly
public void Services_wire_up_correctly()
{
var services = new ServiceCollection();
services.AddEntityFramework().AddSqlServer();
var serviceProvider = services.BuildServiceProvider();
var context = new DbContext(
serviceProvider,
new DbContextOptions().UseSqlServer("goo=boo"));
var scopedProvider = context.Configuration.Services.ServiceProvider;
var databaseBuilder = scopedProvider.GetService<DatabaseBuilder>();
var arrayReaderFactory = scopedProvider.GetService<RelationalObjectArrayValueReaderFactory>();
var typedReaderFactory = scopedProvider.GetService<RelationalTypedValueReaderFactory>();
var batchPreparer = scopedProvider.GetService<CommandBatchPreparer>();
var modificationCommandComparer = scopedProvider.GetService<ModificationCommandComparer>();
var graphFactory = scopedProvider.GetService<GraphFactory>();
var sqlServerDataStoreSource = scopedProvider.GetService<DataStoreSource>() as SqlServerDataStoreSource;
var sqlServerSqlGenerator = scopedProvider.GetService<SqlServerSqlGenerator>();
var sqlStatementExecutor = scopedProvider.GetService<SqlStatementExecutor>();
var sqlTypeMapper = scopedProvider.GetService<SqlServerTypeMapper>();
var sqlServerBatchExecutor = scopedProvider.GetService<SqlServerBatchExecutor>();
var sqlServerModificationCommandBatchFactory = scopedProvider.GetService<ModificationCommandBatchFactory>() as SqlServerModificationCommandBatchFactory;
var sqlServerDataStore = scopedProvider.GetService<SqlServerDataStore>();
var sqlServerConnection = scopedProvider.GetService<SqlServerConnection>();
var modelDiffer = scopedProvider.GetService<ModelDiffer>();
var serverMigrationOperationSqlGeneratorFactory = scopedProvider.GetService<SqlServerMigrationOperationSqlGeneratorFactory>();
var sqlServerDataStoreCreator = scopedProvider.GetService<SqlServerDataStoreCreator>();
var migrationAssembly = scopedProvider.GetService<MigrationAssembly>();
var historyRepository = scopedProvider.GetService<HistoryRepository>();
var sqlServerMigrator = scopedProvider.GetService<Migrator>() as SqlServerMigrator;
Assert.NotNull(databaseBuilder);
Assert.NotNull(arrayReaderFactory);
Assert.NotNull(typedReaderFactory);
Assert.NotNull(batchPreparer);
Assert.NotNull(modificationCommandComparer);
Assert.NotNull(graphFactory);
Assert.NotNull(sqlServerDataStoreSource);
Assert.NotNull(sqlServerSqlGenerator);
Assert.NotNull(sqlStatementExecutor);
Assert.NotNull(sqlTypeMapper);
Assert.NotNull(sqlServerBatchExecutor);
Assert.NotNull(sqlServerModificationCommandBatchFactory);
Assert.NotNull(sqlServerDataStore);
Assert.NotNull(sqlServerConnection);
Assert.NotNull(modelDiffer);
Assert.NotNull(serverMigrationOperationSqlGeneratorFactory);
Assert.NotNull(sqlServerDataStoreCreator);
Assert.NotNull(migrationAssembly);
Assert.NotNull(historyRepository);
Assert.NotNull(sqlServerMigrator);
context.Dispose();
context = new DbContext(
serviceProvider,
new DbContextOptions().UseSqlServer("goo=boo"));
scopedProvider = context.Configuration.Services.ServiceProvider;
// Dingletons
Assert.Same(databaseBuilder, scopedProvider.GetService<DatabaseBuilder>());
Assert.Same(arrayReaderFactory, scopedProvider.GetService<RelationalObjectArrayValueReaderFactory>());
Assert.Same(typedReaderFactory, scopedProvider.GetService<RelationalTypedValueReaderFactory>());
Assert.Same(modificationCommandComparer, scopedProvider.GetService<ModificationCommandComparer>());
Assert.Same(graphFactory, scopedProvider.GetService<GraphFactory>());
Assert.Same(sqlServerSqlGenerator, scopedProvider.GetService<SqlServerSqlGenerator>());
Assert.Same(sqlStatementExecutor, scopedProvider.GetService<SqlStatementExecutor>());
Assert.Same(sqlTypeMapper, scopedProvider.GetService<SqlServerTypeMapper>());
Assert.Same(sqlServerBatchExecutor, scopedProvider.GetService<SqlServerBatchExecutor>());
// Scoped
Assert.NotSame(batchPreparer, scopedProvider.GetService<CommandBatchPreparer>());
Assert.NotSame(sqlServerModificationCommandBatchFactory, scopedProvider.GetService<ModificationCommandBatchFactory>());
Assert.NotSame(sqlServerDataStoreSource, scopedProvider.GetService<DataStoreSource>());
Assert.NotSame(sqlServerDataStore, scopedProvider.GetService<SqlServerDataStore>());
Assert.NotSame(sqlServerConnection, scopedProvider.GetService<SqlServerConnection>());
Assert.NotSame(modelDiffer, scopedProvider.GetService<ModelDiffer>());
Assert.NotSame(serverMigrationOperationSqlGeneratorFactory, scopedProvider.GetService<SqlServerMigrationOperationSqlGeneratorFactory>());
Assert.NotSame(sqlServerDataStoreCreator, scopedProvider.GetService<SqlServerDataStoreCreator>());
Assert.NotSame(migrationAssembly, scopedProvider.GetService<MigrationAssembly>());
Assert.NotSame(historyRepository, scopedProvider.GetService<HistoryRepository>());
Assert.NotSame(sqlServerMigrator, scopedProvider.GetService<Migrator>());
context.Dispose();
}