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();
            }
示例#3
0
        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);
        }
示例#4
0
        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();
        }
示例#6
0
 public static void RemoveAllEntities(CrossStoreContext context)
 => context.SimpleEntities.RemoveRange(context.SimpleEntities);
示例#7
0
        // 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";
        }
示例#8
0
        // 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";
        }
 public static void RemoveAllEntities(CrossStoreContext context) 
     => context.SimpleEntities.RemoveRange(context.SimpleEntities);