public void DbManager_ClearTables() { var connection = (EffortConnection)DbConnectionFactory.CreateTransient(); var model = CompiledModels.GetModel<RelationEntity, EmptyEntity>(); var context = new FeatureDbContext(connection, model); context.Database.Initialize(true); context.RelationEntities.Add( new RelationEntity { RequiredRelation = new EmptyEntity() }); context.SaveChanges(); // Create new context context = new FeatureDbContext(connection, model); // The records should be in context.EmptyEntities.Count().Should().Be(1); context.RelationEntities.Count().Should().Be(1); // Use DbManager to delete all data from the database connection.Open(); connection.DbManager.ClearTables(); // Tables should be empty context.EmptyEntities.Count().Should().Be(0); context.RelationEntities.Count().Should().Be(0); }
public void DbContext_Insert() { DbConnection connection = DbConnectionFactory.CreateTransient(); FeatureDbContext context = new FeatureDbContext(connection); context.StringFieldEntities.Add(new StringFieldEntity { Value = "Foo" }); int count = context.SaveChanges(); Assert.AreEqual(1, count); }
public void DbContext_Query() { DbConnection connection = DbConnectionFactory.CreateTransient(); FeatureDbContext context = new FeatureDbContext(connection); context.StringFieldEntities.Add(new StringFieldEntity { Value = "Foo" }); context.SaveChanges(); StringFieldEntity person = context.StringFieldEntities.Single(); Assert.AreEqual("Foo", person.Value); }
private static void InitializeData(DbConnection connection) { var context = new FeatureDbContext( connection, CompiledModels.GetModel<NumberFieldEntity>()); context.NumberFieldEntities.Add( new NumberFieldEntity { Value8 = 0x0f, Value16 = 0x0f, Value32 = 0x0f, Value64 = 0x0f}); context.SaveChanges(); connection.Close(); }
public void DbManager_SetIdentityFields() { EffortConnection connection = (EffortConnection)DbConnectionFactory.CreateTransient(); FeatureDbContext context = new FeatureDbContext(connection); context.Database.Initialize(true); { // Create a separate context for initializing the data (schema without // identity field) FeatureDbContext dataInitContext = new FeatureDbContext(connection, CompiledModels.DisabledIdentityModel); // DbConfiguration require open connection connection.Open(); // Disable identity fields connection.DbManager.SetIdentityFields(false); // Clear migration history to avoid exception connection.DbManager.ClearMigrationHistory(); // EF cannot handle open connection (fixed in EF6) connection.Close(); // Add data with explicitly set id var initEntity = new StringFieldEntity { Id = 5, Value = "Car" }; dataInitContext.StringFieldEntities.Add(initEntity); dataInitContext.SaveChanges(); // Identity generation should not be used Assert.AreEqual(5, initEntity.Id); // Enable identity field connection.Open(); connection.DbManager.SetIdentityFields(true); connection.Close(); } var entity = new StringFieldEntity { Id = 0, Value = "Bicycle" }; context.StringFieldEntities.Add(entity); context.SaveChanges(); // Identity generation should be used Assert.AreEqual(6, entity.Id); }
public void DbContext_DatabaseExists() { // Microsoft change the way database existence check works // // Exist call with an open connection always returns true // In an earlier version of Effort, the Exists call opened the connection, // so later initialization did not begin DbConnection connection = DbConnectionFactory.CreateTransient(); FeatureDbContext context = new FeatureDbContext(connection); var exists = context.Database.Exists(); Assert.IsFalse(exists); context.StringFieldEntities.Add(new StringFieldEntity { Value = "Foo" }); int count = context.SaveChanges(); Assert.AreEqual(1, count); }
public void CascadedDelete() { this.RelationEntities.Add( new RelationEntity { RequiredRelation = new EmptyEntity() }); this.context.SaveChanges(); // If the entity was removed in the current context, EF would remove the // referrenced entity automatically // If a new context is created, the database would perform the cascaded delete var newContext = new FeatureDbContext(this.context.Database.Connection, this.model); var empty = newContext.EmptyEntities.Single(); newContext.EmptyEntities.Remove(empty); newContext.SaveChanges(); }