private void Verify_DbContext_construction_for_SQLCE(DbContextConstructorArgumentType ctorArguments) { // Act using ( var context = CreateContext<SimpleModelContext>(ctorArguments, providerName: "System.Data.SqlServerCe.4.0")) { // Assert Assert.NotNull(context.Products); Assert.NotNull(context.Categories); context.Assert<Product>().IsInModel(); context.Assert<Category>().IsInModel(); Assert.Equal(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleModel.SimpleModelContext.sdf"), context.Database.Connection.Database); } }
private void DbContext_disposal_behavior_wrt_to_object_context_and_connection( DbContextConstructorArgumentType ctorArguments) { ObjectContext objectContext = null; DbConnection storeConnection = null; using ( var context = CreateContext <SimpleModelContext>(ctorArguments, providerName: "System.Data.SqlServerCe.4.0")) { // Arrange var product = context.Products.Find(1); Assert.NotNull(product); objectContext = GetObjectContext(context); storeConnection = context.Database.Connection; } // Assert if (ctorArguments != DbContextConstructorArgumentType.ObjectContext) { // Assert object context is disposed Assert.Throws <ObjectDisposedException>(() => objectContext.SaveChanges()).ValidateMessage( "ObjectContext_ObjectDisposed"); } if (ctorArguments.Equals(DbContextConstructorArgumentType.Connection) || ctorArguments.Equals(DbContextConstructorArgumentType.ConnectionAndDbCompiledModel)) { // Assert that connection is closed but not disposed Assert.True( storeConnection.State == ConnectionState.Closed && !storeConnection.ConnectionString.Equals(string.Empty)); } else { // Assert connection is disposed Assert.True( storeConnection.State == ConnectionState.Closed && storeConnection.ConnectionString.Equals(string.Empty)); } }
private void Verify_DbContext_construction_for_SQLCE(DbContextConstructorArgumentType ctorArguments) { // Act using ( var context = CreateContext <SimpleModelContext>( ctorArguments, providerName: "System.Data.SqlServerCe.4.0")) { // Assert Assert.NotNull(context.Products); Assert.NotNull(context.Categories); context.Assert <Product>().IsInModel(); context.Assert <Category>().IsInModel(); Assert.Equal( Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleModel.SimpleModelContext.sdf"), context.Database.Connection.Database); } }
private void DbContext_disposal_behavior_wrt_to_object_context_and_connection( DbContextConstructorArgumentType ctorArguments) { ObjectContext objectContext = null; DbConnection storeConnection = null; using ( var context = CreateContext<SimpleModelContext>(ctorArguments, providerName: "System.Data.SqlServerCe.4.0")) { // Arrange var product = context.Products.Find(1); Assert.NotNull(product); objectContext = GetObjectContext(context); storeConnection = context.Database.Connection; } // Assert if (ctorArguments != DbContextConstructorArgumentType.ObjectContext) { // Assert object context is disposed Assert.Throws<ObjectDisposedException>(() => objectContext.SaveChanges()).ValidateMessage( "ObjectContext_ObjectDisposed"); } if (ctorArguments.Equals(DbContextConstructorArgumentType.Connection) || ctorArguments.Equals(DbContextConstructorArgumentType.ConnectionAndDbCompiledModel)) { // Assert that connection is closed but not disposed Assert.True( storeConnection.State == ConnectionState.Closed && !storeConnection.ConnectionString.Equals(string.Empty)); } else { // Assert connection is disposed Assert.True( storeConnection.State == ConnectionState.Closed && storeConnection.ConnectionString.Equals(string.Empty)); } }
protected static TContext CreateContext <TContext>( DbContextConstructorArgumentType arguments, string providerName = "System.Data.SqlClient") where TContext : DbContext { DbConnection connection = null; if (arguments == DbContextConstructorArgumentType.Connection || arguments == DbContextConstructorArgumentType.ConnectionAndDbCompiledModel) { if (providerName == "System.Data.SqlClient") { connection = SimpleConnection <TContext>(); } else if (providerName == "System.Data.SqlServerCe.4.0") { connection = SimpleCeConnection <TContext>(); } else { throw new ArgumentException("Invalid provider specified, " + providerName); } } string connectionString = null; if (arguments == DbContextConstructorArgumentType.ConnectionString || arguments == DbContextConstructorArgumentType.ConnectionStringAndDbCompiledModel) { if (providerName == "System.Data.SqlClient") { connectionString = SimpleConnectionString <TContext>(); } else if (providerName == "System.Data.SqlServerCe.4.0") { connectionString = SimpleCeConnectionString <TContext>(); } else { throw new ArgumentException("Invalid provider specified, " + providerName); } } var providerInfo = (providerName == "System.Data.SqlServerCe.4.0") ? ProviderRegistry.SqlCe4_ProviderInfo : ProviderRegistry.Sql2008_ProviderInfo; TContext context = null; switch (arguments) { case DbContextConstructorArgumentType.Parameterless: context = (TContext)Activator.CreateInstance(typeof(TContext)); break; case DbContextConstructorArgumentType.DbCompiledModel: context = (TContext) Activator.CreateInstance( typeof(TContext), SimpleModelContext.CreateBuilder().Build(providerInfo).Compile()); break; case DbContextConstructorArgumentType.Connection: context = (TContext)Activator.CreateInstance(typeof(TContext), connection, false); break; case DbContextConstructorArgumentType.ConnectionString: context = (TContext)Activator.CreateInstance(typeof(TContext), connectionString); break; case DbContextConstructorArgumentType.ConnectionAndDbCompiledModel: context = (TContext) Activator.CreateInstance( typeof(TContext), connection, SimpleModelContext.CreateBuilder().Build(connection).Compile(), false); break; case DbContextConstructorArgumentType.ConnectionStringAndDbCompiledModel: context = (TContext) Activator.CreateInstance( typeof(TContext), connectionString, SimpleModelContext.CreateBuilder().Build(providerInfo).Compile()); break; default: throw new ArgumentException("Invalid DbContext constructor arguments " + arguments); } return(context); }