示例#1
0
        public async Task Empty_Migration_Creates_Database()
        {
            using (var context = new BloggingContext(
                       Fixture.TestStore.AddProviderOptions(
                           new DbContextOptionsBuilder().EnableServiceProviderCaching(false)).Options))
            {
                var creator = (SqlServerDatabaseCreator)context.GetService <IRelationalDatabaseCreator>();
                creator.RetryTimeout = TimeSpan.FromMinutes(10);

                await context.Database.MigrateAsync();

                Assert.True(creator.Exists());
            }
        }
示例#2
0
        private static async Task CreateTables_creates_schema_in_existing_database_test(bool async)
        {
            using (var testDatabase = SqlCeTestStore.CreateScratch(createDatabase: true))
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection
                .AddEntityFrameworkSqlCe();

                var serviceProvider = serviceCollection.BuildServiceProvider();

                var optionsBuilder = new DbContextOptionsBuilder();
                optionsBuilder.UseSqlCe(testDatabase.ConnectionString);

                using (var context = new BloggingContext(optionsBuilder.Options))
                {
                    var creator = (RelationalDatabaseCreator)context.GetService <IDatabaseCreator>();

                    if (async)
                    {
                        await creator.CreateTablesAsync();
                    }
                    else
                    {
                        creator.CreateTables();
                    }

                    if (testDatabase.ConnectionState != ConnectionState.Open)
                    {
                        await testDatabase.OpenConnectionAsync();
                    }

                    var tables = testDatabase.Query <string>("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
                    Assert.Equal(1, tables.Count());
                    Assert.Equal("Blogs", tables.Single());

                    var columns = testDatabase.Query <string>("SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS");
                    Assert.Equal(2, columns.Count());
                    Assert.True(columns.Any(c => c == "Blogs.Id"));
                    Assert.True(columns.Any(c => c == "Blogs.Name"));
                }
            }
        }
        private static async Task EnsureDeleted_will_delete_database_test(bool async, bool openConnection)
        {
            using (var testDatabase = await SqlCeTestStore.CreateScratchAsync(createDatabase: true))
            {
                if (!openConnection)
                {
                    testDatabase.CloseConnection();
                }

                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = context.GetService <IRelationalDatabaseCreator>();

                    Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                    if (openConnection)
                    {
                        Assert.Throws <IOException>(() => context.Database.EnsureDeleted());
                    }
                    else
                    {
                        if (async)
                        {
                            Assert.True(await context.Database.EnsureDeletedAsync());
                        }
                        else
                        {
                            Assert.True(context.Database.EnsureDeleted());
                        }

                        Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                        Assert.False(async ? await creator.ExistsAsync() : creator.Exists());

                        Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                    }
                }
            }
        }