コード例 #1
0
        public static IEntityOptions CreateOptions()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=SqlServerConnectionTest;Trusted_Connection=True;");

            return optionsBuilder.Options;
        }
コード例 #2
0
        public OneToOneQuerySqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlServer()
                  .ServiceCollection()
                  .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                  .AddInstance <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            var database = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(database.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();

                AddTestData(context);
            }
        }
コード例 #3
0
        private static void CreateTestStore <TContext>(
            string databaseName,
            IServiceProvider serviceProvider,
            Func <IServiceProvider, EntityOptions, TContext> contextCreator,
            Action <TContext> contextInitializer)
            where TContext : DbContext, IDisposable
        {
            var connectionString = SqlServerTestStore.CreateConnectionString(databaseName);

            SqlServerTestStore.GetOrCreateShared(databaseName, () =>
            {
                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseSqlServer(connectionString);

                using (var context = contextCreator(serviceProvider, optionsBuilder.Options))
                {
                    if (context.Database.EnsureCreated())
                    {
                        contextInitializer(context);
                    }

                    TestSqlLoggerFactory.SqlStatements.Clear();
                }
            });
        }
コード例 #4
0
        public void Is_configured_when_configuration_contains_associated_extension()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie");

            Assert.True(new SqlServerDataStoreSource().IsConfigured(optionsBuilder.Options));
        }
コード例 #5
0
        public void Batches_are_divided_correctly_with_two_inserted_columns()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(_testStore.Connection);

            using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
            {
                context.Database.EnsureCreated();

                for (var i = 1; i < 1101; i++)
                {
                    var blog = new Blog {
                        Id = i, Name = "Foo" + i
                    };
                    context.Blogs.Add(blog);
                }

                context.SaveChanges();
            }

            using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
            {
                Assert.Equal(1100, context.Blogs.Count());
            }
        }
コード例 #6
0
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     if (!string.IsNullOrWhiteSpace(_connectionString))
     {
         optionsBuilder.UseSqlServer(_connectionString);
     }
 }
コード例 #7
0
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            var connString = Startup.Configuration["Data:WorldContextConnection"];
            optionsBuilder.UseSqlServer(connString);

            base.OnConfiguring(optionsBuilder);
        }
コード例 #8
0
        public static IEntityOptions CreateOptions()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=SqlServerConnectionTest;Trusted_Connection=True;");

            return(optionsBuilder.Options);
        }
コード例 #9
0
            protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
            {
                Assert.Same(_options, optionsBuilder.Options);

                optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                Assert.NotSame(_options, optionsBuilder.Options);
            }
コード例 #10
0
            protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
            {
                Assert.Same(_options, optionsBuilder.Options);

                optionsBuilder.UseSqlServer(_connection);

                Assert.NotSame(_options, optionsBuilder.Options);
            }
コード例 #11
0
        protected override EntityOptions CreateOptions(string databaseName)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(CreateConnectionString(databaseName));

            return(optionsBuilder.Options);
        }
コード例 #12
0
        public override DbContext CreateContext(DbConnection connection)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(connection);

            return(new DbContext(_serviceProvider, optionsBuilder.Options));
        }
コード例 #13
0
        public override DbContext CreateContext(SqlServerTestStore testStore)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(testStore.Connection.ConnectionString);

            return(new DbContext(_serviceProvider, optionsBuilder.Options));
        }
コード例 #14
0
        public void Is_configured_when_configuration_contains_associated_extension()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie");

            Assert.True(new SqlServerDataStoreSource().IsConfigured(optionsBuilder.Options));
        }
コード例 #15
0
        public async Task Can_save_changes_in_tracked_entities()
        {
            using (var testDatabase = await SqlServerTestStore.CreateScratchAsync())
            {
                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseSqlServer(testDatabase.Connection.ConnectionString);

                int updatedId;
                int deletedId;
                int addedId;
                using (var db = new BloggingContext(_fixture.ServiceProvider, optionsBuilder.Options))
                {
                    var blogs = await CreateBlogDatabaseAsync <Blog>(db);

                    var toAdd = db.Blogs.Add(new Blog
                    {
                        Name       = "Blog to Insert",
                        George     = true,
                        TheGu      = new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"),
                        NotFigTime = new DateTime(1973, 9, 3, 0, 10, 33, 777),
                        ToEat      = 64,
                        OrNothing  = 0.123456789,
                        Fuse       = 777,
                        WayRound   = 9876543210,
                        Away       = 0.12345f,
                        AndChew    = new byte[16]
                    }).Entity;
                    db.Entry(toAdd).State = EntityState.Detached;

                    var toUpdate = blogs[0];
                    toUpdate.Name = "Blog is Updated";
                    updatedId     = toUpdate.Id;
                    var toDelete = blogs[1];
                    toDelete.Name = "Blog to delete";
                    deletedId     = toDelete.Id;

                    db.Remove(toDelete);
                    db.Entry(toAdd).State = EntityState.Added;

                    await db.SaveChangesAsync();

                    addedId = toAdd.Id;
                    Assert.NotEqual(0, addedId);

                    Assert.Equal(EntityState.Unchanged, db.Entry(toUpdate).State);
                    Assert.Equal(EntityState.Unchanged, db.Entry(toAdd).State);
                    Assert.DoesNotContain(toDelete, db.ChangeTracker.Entries().Select(e => e.Entity));
                }

                using (var db = new BloggingContext(_fixture.ServiceProvider, optionsBuilder.Options))
                {
                    var toUpdate = db.Blogs.Single(b => b.Id == updatedId);
                    Assert.Equal("Blog is Updated", toUpdate.Name);
                    Assert.Equal(0, db.Blogs.Count(b => b.Id == deletedId));
                    Assert.Equal("Blog to Insert", db.Blogs.Single(b => b.Id == addedId).Name);
                }
            }
        }
コード例 #16
0
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
            // Seems for now EF7 can't support named connection string like "name=NorthwindConnection",
            // find an equivalent approach when it's ready.
            optionsBuilder.UseSqlServer(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework");

            base.OnConfiguring(optionsBuilder);
        }
コード例 #17
0
ファイル: NorthwindContext.cs プロジェクト: karataliu/RESTier
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            // TODO GitHubIssue#57: Complete EF7 to EDM model mapping
            // Seems for now EF7 can't support named connection string like "name=NorthwindConnection",
            // find an equivalent approach when it's ready.
            optionsBuilder.UseSqlServer(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework");

            base.OnConfiguring(optionsBuilder);
        }
コード例 #18
0
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            var sqlBuilder = optionsBuilder.UseSqlServer(_connectionString);

            if (_disableBatching)
            {
                sqlBuilder.MaxBatchSize(1);
            }
        }
        public void Can_add_extension_with_max_batch_size()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie").MaxBatchSize(123);

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Equal(123, extension.MaxBatchSize);
        }
        public void Can_add_extension_with_command_timeout()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie").CommandTimeout(30);

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Equal(30, extension.CommandTimeout);
        }
        public void Can_add_extension_with_ambient_transaction_warning_suppressed()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie").SuppressAmbientTransactionWarning();

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Equal(false, extension.ThrowOnAmbientTransaction);
        }
コード例 #22
0
        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    base.OnConfiguring(optionsBuilder);
        //    optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
        //}
        //EntityOptionsBuilder
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            //var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test.db" };
            //var connectionString = connectionStringBuilder.ToString();
            //var connection = new SqliteConnection(connectionString);

            //optionsBuilder.UseSqlite(connection);
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
        }
コード例 #23
0
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            var sqlBuilder = optionsBuilder.UseSqlServer(_connectionString);

            if (_disableBatching)
            {
                sqlBuilder.MaxBatchSize(1);
            }
        }
コード例 #24
0
        public void Can_add_extension_with_ambient_transaction_warning_suppressed()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie").SuppressAmbientTransactionWarning();

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Equal(false, extension.ThrowOnAmbientTransaction);
        }
        public void Can_add_extension_with_connection_string()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie");

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Equal("Database=Crunchie", extension.ConnectionString);
            Assert.Null(extension.Connection);
        }
コード例 #26
0
        public void Can_add_extension_with_max_batch_size()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie").MaxBatchSize(123);

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Equal(123, extension.MaxBatchSize);
        }
コード例 #27
0
        public void Can_add_extension_with_command_timeout()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie").CommandTimeout(30);

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Equal(30, extension.CommandTimeout);
        }
        public void Can_add_extension_with_connection_string_using_generic_options()
        {
            var optionsBuilder = new EntityOptionsBuilder<DbContext>();
            optionsBuilder.UseSqlServer("Database=Whisper");

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Equal("Database=Whisper", extension.ConnectionString);
            Assert.Null(extension.Connection);
        }
コード例 #29
0
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            //var connString = Startup.Configuration["Data:ClaimContextConnection"];
            //var connString = "Server=(localdb)\\ProjectsV12;Database=ClaimDB;Trusted_Connection=;MultipleActiveResultSets=true;";

            var connString = "Server=(localdb)\\v11.0;Database=ClaimDB2;Trusted_Connection=true;MultipleActiveResultSets=true;";
            optionsBuilder.UseSqlServer(connString);

            base.OnConfiguring(optionsBuilder);
        }
コード例 #30
0
        public virtual ISqlServerConnection CreateMasterConnection()
        {
            var builder = new SqlConnectionStringBuilder { ConnectionString = ConnectionString, InitialCatalog = "master" };

            // TODO use clone connection method once implimented see #1406
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(builder.ConnectionString).CommandTimeout(CommandTimeout);

            return new SqlServerConnection(optionsBuilder.Options, _loggerFactory);
        }
コード例 #31
0
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     if (UseSqlServer)
     {
         optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);
     }
     else
     {
         optionsBuilder.UseInMemoryStore();
     }
 }
コード例 #32
0
ファイル: Model.cs プロジェクト: rhires/EntityFramework.Docs
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            // Visual Studio 2015 | Use the LocalDb 12 instance created by Visual Studio
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

            // Visual Studio 2013 | Use the LocalDb 11 instance created by Visual Studio
            // optionsBuilder.UseSqlServer(@"Server=(localdb)\v11.0;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");

            // Visual Studio 2012 | Use the SQL Express instance created by Visual Studio
            // optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
        }
コード例 #33
0
        public void Can_add_extension_with_connection_string_using_generic_options()
        {
            var optionsBuilder = new EntityOptionsBuilder <DbContext>();

            optionsBuilder.UseSqlServer("Database=Whisper");

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Equal("Database=Whisper", extension.ConnectionString);
            Assert.Null(extension.Connection);
        }
コード例 #34
0
        public void Can_add_extension_with_connection_string()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie");

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Equal("Database=Crunchie", extension.ConnectionString);
            Assert.Null(extension.Connection);
        }
コード例 #35
0
        public override GearsOfWarContext CreateContext(SqlServerTestStore testStore)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(testStore.Connection);

            var context = new GearsOfWarContext(_serviceProvider, optionsBuilder.Options);

            context.Database.AsRelational().Connection.UseTransaction(testStore.Transaction);
            return(context);
        }
        public void SqlServerOptionsExtension_is_optional()
        {
            var factory = new SqlServerModificationCommandBatchFactory(new SqlServerSqlGenerator());

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer("Database=Crunchie");

            var batch = factory.Create(optionsBuilder.Options, new SqlServerMetadataExtensionProvider());

            Assert.True(factory.AddCommand(batch, new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.SqlServer(), new UntypedValueBufferFactoryFactory())));
            Assert.True(factory.AddCommand(batch, new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.SqlServer(), new UntypedValueBufferFactoryFactory())));
        }
コード例 #37
0
        public void Can_add_extension_with_connection_using_generic_options()
        {
            var optionsBuilder = new EntityOptionsBuilder <DbContext>();
            var connection     = new SqlConnection();

            optionsBuilder.UseSqlServer(connection);

            var extension = optionsBuilder.Options.Extensions.OfType <SqlServerOptionsExtension>().Single();

            Assert.Same(connection, extension.Connection);
            Assert.Null(extension.ConnectionString);
        }
        public void Can_add_extension_with_connection()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            var connection = new SqlConnection();

            optionsBuilder.UseSqlServer(connection);

            var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

            Assert.Same(connection, extension.Connection);
            Assert.Null(extension.ConnectionString);
        }
        public void SqlServerOptionsExtension_is_optional()
        {
            var factory = new SqlServerModificationCommandBatchFactory(new SqlServerSqlGenerator());

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer("Database=Crunchie");

            var batch = factory.Create(optionsBuilder.Options, new SqlServerMetadataExtensionProvider());

            Assert.True(factory.AddCommand(batch, new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.SqlServer(), new UntypedValueBufferFactoryFactory())));
            Assert.True(factory.AddCommand(batch, new ModificationCommand("T1", null, new ParameterNameGenerator(), p => p.SqlServer(), new UntypedValueBufferFactoryFactory())));
        }
コード例 #40
0
        public virtual ISqlServerConnection CreateMasterConnection()
        {
            var builder = new SqlConnectionStringBuilder {
                ConnectionString = ConnectionString, InitialCatalog = "master"
            };

            // TODO use clone connection method once implimented see #1406
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(builder.ConnectionString).CommandTimeout(CommandTimeout);

            return(new SqlServerConnection(optionsBuilder.Options, _loggerFactory));
        }
コード例 #41
0
            public async Task Can_pass_context_options_to_constructor_and_use_in_builder()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var optionsBuilder = new EntityOptionsBuilder();
                    optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                    using (var context = new NorthwindContext(optionsBuilder.Options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
コード例 #42
0
        private void EnsureDeleted()
        {
            if (!PlatformHelper.IsMono)
            {
                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseSqlServer(_connectionString);

                using (var db = new DbContext(optionsBuilder.Options))
                {
                    db.Database.EnsureDeleted();
                }
            }
        }
コード例 #43
0
            public async Task Can_query_with_implicit_services_and_explicit_config()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var optionsBuilder = new EntityOptionsBuilder();
                    optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                    using (var context = new NorthwindContext(optionsBuilder.Options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
コード例 #44
0
            public async Task Can_query_with_implicit_services_and_explicit_config()
            {
                using (await SqlServerNorthwindContext.GetSharedStoreAsync())
                {
                    var optionsBuilder = new EntityOptionsBuilder();
                    optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);

                    using (var context = new NorthwindContext(optionsBuilder.Options))
                    {
                        Assert.Equal(91, await context.Customers.CountAsync());
                    }
                }
            }
コード例 #45
0
        private static BloggingContext CreateContext(SqlServerTestStore testStore)
        {
            var serviceProvider =
                new ServiceCollection()
                    .AddEntityFramework()
                    .AddSqlServer()
                    .ServiceCollection()
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(testStore.Connection.ConnectionString);

            return new BloggingContext(serviceProvider, optionsBuilder.Options);
        }
コード例 #46
0
            public override SqlServerTestStore CreateTestStore()
            {
                return(SqlServerTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new EntityOptionsBuilder();
                    optionsBuilder.UseSqlServer(SqlServerTestStore.CreateConnectionString(DatabaseName));

                    using (var context = new StoreGeneratedContext(_serviceProvider, optionsBuilder.Options))
                    {
                        context.Database.EnsureDeleted();
                        context.Database.EnsureCreated();
                    }
                }));
            }
コード例 #47
0
        public MappingQuerySqlServerFixture()
        {
            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                .BuildServiceProvider();

            _testDatabase = SqlServerNorthwindContext.GetSharedStore();

            var optionsBuilder = new EntityOptionsBuilder().UseModel(CreateModel());
            optionsBuilder.UseSqlServer(_testDatabase.Connection.ConnectionString);
            _options = optionsBuilder.Options;
        }
コード例 #48
0
        private static BloggingContext CreateContext(SqlServerTestStore testStore)
        {
            var serviceProvider =
                new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseSqlServer(testStore.Connection.ConnectionString);

            return(new BloggingContext(serviceProvider, optionsBuilder.Options));
        }
コード例 #49
0
ファイル: AssemblyContext.cs プロジェクト: anttieskola/tsdemo
 protected override void OnConfiguring(EntityOptionsBuilder eob)
 {
     if (!eob.IsConfigured)
     {
         // find configuration for command line EF tool
         var builder = new ConfigurationBuilder("../tsdemo.ui/")
             .AddJsonFile("config.json")
             .AddEnvironmentVariables();
         IConfiguration configuration = builder.Build();
         // setup server connection string
         var connString = configuration["ConnectionString:localhost"];
         eob.UseSqlServer(connString);
     }
     base.OnConfiguring(eob);
 }
コード例 #50
0
            public NullKeysSqlServerFixture()
            {
                _serviceProvider = new ServiceCollection()
                                   .AddEntityFramework()
                                   .AddSqlServer()
                                   .ServiceCollection()
                                   .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                                   .BuildServiceProvider();

                var optionsBuilder = new EntityOptionsBuilder();

                optionsBuilder.UseSqlServer(SqlServerTestStore.CreateConnectionString("StringsContext"));
                _options = optionsBuilder.Options;

                EnsureCreated();
            }
コード例 #51
0
        public MappingQuerySqlServerFixture()
        {
            _serviceProvider = new ServiceCollection()
                               .AddEntityFramework()
                               .AddSqlServer()
                               .ServiceCollection()
                               .AddInstance <ILoggerFactory>(new TestSqlLoggerFactory())
                               .BuildServiceProvider();

            _testDatabase = SqlServerNorthwindContext.GetSharedStore();

            var optionsBuilder = new EntityOptionsBuilder().UseModel(CreateModel());

            optionsBuilder.UseSqlServer(_testDatabase.Connection.ConnectionString);
            _options = optionsBuilder.Options;
        }
        public NorthwindSprocQuerySqlServerFixture()
        {
            _testStore = SqlServerNorthwindContext.GetSharedStore();

            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(_testStore.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            _serviceProvider.GetRequiredService<ILoggerFactory>()
                .MinimumLevel = LogLevel.Debug;
        }
コード例 #53
0
        public InheritanceSqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddSqlServer()
                    .ServiceCollection()
                    .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                    .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                    .BuildServiceProvider();

            var testStore = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(testStore.Connection);
            _options = optionsBuilder.Options;

            // TODO: Do this via migrations

            testStore.ExecuteNonQuery(@"
                CREATE TABLE Country (
                    Id int NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL
                );

                CREATE TABLE Animal (
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NOT NULL FOREIGN KEY REFERENCES Country (Id),
                    IsFlightless bit NOT NULL,
                    EagleId nvarchar(100) FOREIGN KEY REFERENCES Animal (Species),
                    [Group] int,
                    FoundOn tinyint,
                    Discriminator nvarchar(255) NOT NULL
                );");

            using (var context = CreateContext())
            {
                SeedData(context);
            }
        }
コード例 #54
0
        public BuiltInDataTypesSqlServerFixture()
        {
            _testStore = SqlServerTestStore.CreateScratch();

            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddSqlServer()
                .ServiceCollection()
                .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(_testStore.Connection);

            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();
            }
        }
コード例 #55
0
        public override CrossStoreContext CreateContext(TestStore testStore)
        {
            var inMemoryTestStore = testStore as InMemoryTestStore;
            if (inMemoryTestStore != null)
            {
                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseInMemoryStore();

                return new CrossStoreContext(_serviceProvider, optionsBuilder.Options);
            }

            var sqliteTestStore = testStore as SqliteTestStore;
            if (sqliteTestStore != null)
            {
                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseSqlite(sqliteTestStore.Connection);

                var context = new CrossStoreContext(_serviceProvider, optionsBuilder.Options);
                context.Database.EnsureCreated();
                context.Database.AsRelational().Connection.UseTransaction(sqliteTestStore.Transaction);

                return context;
            }

            var sqlServerTestStore = testStore as SqlServerTestStore;
            if (sqlServerTestStore != null)
            {
                var optionsBuilder = new EntityOptionsBuilder();
                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();
        }
コード例 #56
0
        public void Batches_are_divided_correctly_with_two_inserted_columns()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(_testStore.Connection);

            using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
            {
                context.Database.EnsureCreated();

                for (var i = 1; i < 1101; i++)
                {
                    var blog = new Blog { Id = i, Name = "Foo" + i };
                    context.Blogs.Add(blog);
                }

                context.SaveChanges();
            }

            using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
            {
                Assert.Equal(1100, context.Blogs.Count());
            }
        }
コード例 #57
0
        public OneToOneQuerySqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddSqlServer()
                    .ServiceCollection()
                    .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                    .AddInstance<ILoggerFactory>(new TestSqlLoggerFactory())
                    .BuildServiceProvider();

            var database = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(database.Connection.ConnectionString);
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();

                AddTestData(context);
            }
        }
コード例 #58
0
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseSqlServer(SqlServerTestStore.CreateConnectionString(_databaseName));
 }
コード例 #59
0
        private static IServiceProvider CreateContextServices(SqlServerTestStore testStore)
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection
                .AddEntityFramework()
                .AddSqlServer();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseSqlServer(testStore.Connection.ConnectionString);

            return ((IAccessor<IServiceProvider>)new DbContext(
                serviceCollection.BuildServiceProvider(),
                optionsBuilder.Options))
                .Service;
        }
コード例 #60
0
        private static async Task CreateTables_creates_schema_in_existing_database_test(bool async)
        {
            using (var testDatabase = await SqlServerTestStore.CreateScratchAsync())
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection
                    .AddEntityFramework()
                    .AddSqlServer();

                var serviceProvider = serviceCollection.BuildServiceProvider();

                var optionsBuilder = new EntityOptionsBuilder();
                optionsBuilder.UseSqlServer(testDatabase.Connection.ConnectionString);

                using (var context = new BloggingContext(serviceProvider, optionsBuilder.Options))
                {
                    var contextServices = ((IAccessor<IServiceProvider>)context).Service;

                    var creator = (RelationalDataStoreCreator)contextServices.GetRequiredService<IDataStoreCreator>();

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

                    if (testDatabase.Connection.State != ConnectionState.Open)
                    {
                        await testDatabase.Connection.OpenAsync();
                    }

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

                    var columns = await testDatabase.QueryAsync<string>("SELECT TABLE_NAME + '.' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS");
                    Assert.Equal(2, columns.Count());
                    Assert.True(columns.Any(c => c == "Blog.Id"));
                    Assert.True(columns.Any(c => c == "Blog.Name"));
                }
            }
        }