public override CrossStoreContext CreateContext(TestStore testStore) { var inMemoryTestStore = testStore as InMemoryTestStore; if (inMemoryTestStore != null) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseInMemoryStore(); return(new CrossStoreContext(_serviceProvider, optionsBuilder.Options)); } var sqlServerTestStore = testStore as SqlServerTestStore; if (sqlServerTestStore != null) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(sqlServerTestStore.Connection); var context = new CrossStoreContext(_serviceProvider, optionsBuilder.Options); context.Database.EnsureCreated(); context.Database.AsRelational().Connection.UseTransaction(sqlServerTestStore.Transaction); return(context); } throw new NotImplementedException(); }
private void Seed(CrossStoreContext context) { context.SimpleEntities.Add(new SimpleEntity { StringProperty = "Entity 1" }); context.SaveChanges(); }
public override CrossStoreContext CreateContext(SqlServerTestStore testStore) { var options = new DbContextOptions() .UseSqlServer(testStore.Connection); var context = new CrossStoreContext(_serviceProvider, options); context.Database.EnsureCreated(); context.Database.AsRelational().Connection.UseTransaction(testStore.Transaction); return(context); }
public virtual void Can_save_changes_and_query() { int secondId; using (var context = CreateContext()) { context.SimpleEntities.Add( new SimpleEntity { StringProperty = "Entity 1" }); Assert.Equal(1, context.SaveChanges()); var second = context.SimpleEntities.Add( new SimpleEntity { StringProperty = "Entity 2" }).Entity; context.Entry(second).Property(SimpleEntity.ShadowPropertyName).CurrentValue = "shadow"; Assert.Equal(1, context.SaveChanges()); secondId = second.Id; } using (var context = CreateContext()) { Assert.Equal(2, context.SimpleEntities.Count()); var firstEntity = context.SimpleEntities.Single(e => e.StringProperty == "Entity 1"); var secondEntity = context.SimpleEntities.Single(e => e.Id == secondId); Assert.Equal("Entity 2", secondEntity.StringProperty); var thirdEntity = context.SimpleEntities.Single(e => EF.Property <string>(e, SimpleEntity.ShadowPropertyName) == "shadow"); Assert.Same(secondEntity, thirdEntity); firstEntity.StringProperty = "first"; context.SimpleEntities.Remove(secondEntity); Assert.Equal(2, context.SaveChanges()); } using (var context = CreateContext()) { Assert.Equal("first", context.SimpleEntities.Single().StringProperty); CrossStoreContext.RemoveAllEntities(context); context.SaveChanges(); } }
public override CrossStoreContext CreateContext(TestStore testStore) { var inMemoryTestStore = testStore as InMemoryTestStore; if (inMemoryTestStore != null) { var optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase() .UseInternalServiceProvider(_serviceProvider); return(new CrossStoreContext(optionsBuilder.Options)); } var sqliteTestStore = testStore as SqliteTestStore; if (sqliteTestStore != null) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlite(sqliteTestStore.Connection); var context = new CrossStoreContext(optionsBuilder.Options); context.Database.EnsureCreated(); context.Database.UseTransaction(sqliteTestStore.Transaction); return(context); } var sqlServerTestStore = testStore as SqlServerTestStore; if (sqlServerTestStore != null) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(sqlServerTestStore.Connection, b => b.ApplyConfiguration()); var context = new CrossStoreContext(optionsBuilder.Options); context.Database.EnsureCreated(); context.Database.UseTransaction(sqlServerTestStore.Transaction); return(context); } throw new NotImplementedException(); }
public static void RemoveAllEntities(CrossStoreContext context) => context.SimpleEntities.RemoveRange(context.SimpleEntities);
// TODO: Use a value generator to handle this automatically private void SetPartitionId(SimpleEntity entity, CrossStoreContext context) { var property = context.Model.GetEntityType(entity.GetType()).GetProperty(SimpleEntity.ShadowPartitionIdName); context.Entry(entity).GetService()[property] = "Partition"; }
// TODO: Use a value generator to handle this automatically private void SetPartitionId(SimpleEntity entity, CrossStoreContext context) { var property = context.Model.FindEntityType(entity.GetType()).FindProperty(SimpleEntity.ShadowPartitionIdName); context.Entry(entity).GetInfrastructure()[property] = "Partition"; }
// TODO: Use a value generator to handle this automatically private void SetPartitionId(SimpleEntity entity, CrossStoreContext context) { var property = context.Model.GetEntityType(entity.GetType()).GetProperty(SimpleEntity.ShadowPartitionIdName); ((IAccessor <InternalEntityEntry>)context.Entry(entity)).Service[property] = "Partition"; }