Example #1
0
        public InheritanceSqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlServer()
                  .ServiceCollection()
                  .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                  .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            _testStore = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder
            .EnableSensitiveDataLogging()
            .UseSqlServer(_testStore.Connection);

            _options = optionsBuilder.Options;
            using (var context = CreateContext())
            {
                context.Database.EnsureCreated();
                SeedData(context);
            }
        }
        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);
            }
        }
 public BatchingTest()
 {
     _testStore       = SqlServerTestStore.CreateScratch();
     _serviceProvider = new ServiceCollection()
                        .AddEntityFramework()
                        .AddSqlServer()
                        .ServiceCollection()
                        .BuildServiceProvider();
 }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                var optionBuilder = optionsBuilder.UseSqlServer(SqlServerTestStore.CreateScratch().Connection);

                if (_rowNumberPaging)
                {
                    optionBuilder.UseRowNumberForPaging();
                }
            }
            public virtual SqlServerTestStore GetTestStore()
            {
                var testStore = SqlServerTestStore.CreateScratch();

                using (var context = CreateContext(testStore))
                {
                    context.Database.EnsureCreated();

                    testStore.ExecuteNonQuery(@"
CREATE TRIGGER TRG_InsertProduct
ON Product
AFTER INSERT AS
BEGIN
	if @@ROWCOUNT = 0
		return
	set nocount on;

    INSERT INTO ProductBackup
    SELECT * FROM INSERTED;
END");

                    testStore.ExecuteNonQuery(@"
CREATE TRIGGER TRG_UpdateProduct
ON Product
AFTER UPDATE AS
BEGIN
	if @@ROWCOUNT = 0
		return
	set nocount on;

    DELETE FROM ProductBackup
    WHERE Id IN(SELECT DELETED.Id FROM DELETED);

    INSERT INTO ProductBackup
    SELECT * FROM INSERTED;
END");

                    testStore.ExecuteNonQuery(@"
CREATE TRIGGER TRG_DeleteProduct
ON Product
AFTER DELETE AS
BEGIN
	if @@ROWCOUNT = 0
		return
	set nocount on;

    DELETE FROM ProductBackup
    WHERE Id IN(SELECT DELETED.Id FROM DELETED);
END");
                }

                return(testStore);
            }
        public InheritanceSqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlServer()
                  .ServiceCollection()
                  .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                  .AddInstance <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            var testStore = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder
            .EnableSensitiveDataLogging()
            .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
                );

                CREATE TABLE Plant(
                    Genus int NOT NULL,
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int FOREIGN KEY REFERENCES Country (Id),
                    HasThorns bit
                );");

            using (var context = CreateContext())
            {
                SeedData(context);
            }
        }
Example #7
0
        public void Query_when_null_key_in_database_should_throw()
        {
            using (var testStore = SqlServerTestStore.CreateScratch())
            {
                testStore.ExecuteNonQuery(
                    @"CREATE TABLE ZeroKey (Id int);
                      INSERT ZeroKey VALUES (NULL)");

                using (var context = new NullKeyContext(testStore.Connection.ConnectionString))
                {
                    Assert.Equal(
                        RelationalStrings.InvalidKeyValue("ZeroKey"),
                        Assert.Throws <InvalidOperationException>(() => context.ZeroKeys.ToList()).Message);
                }
            }
        }
        public InheritanceSqlServerFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlServer()
                  .ServiceCollection()
                  .AddSingleton(TestSqlServerModelSource.GetFactory(OnModelCreating))
                  .AddInstance <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            var testStore = SqlServerTestStore.CreateScratch();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer(testStore.Connection);
            _options = optionsBuilder.Options;

            // TODO: Do this via migrations & update pipeline

            testStore.ExecuteNonQueryAsync(@"
                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)
                );
                
                INSERT Country VALUES (1, 'New Zealand');
                INSERT Country VALUES (2, 'USA');

                INSERT Animal VALUES ('Aquila chrysaetos canadensis', 'American golden eagle', 2, 0, NULL, 1, NULL, 'Eagle');
                INSERT Animal VALUES ('Apteryx owenii', 'Great spotted kiwi', 1, 1, 'Aquila chrysaetos canadensis', NULL, 1, 'Kiwi');
            ").Wait();
        }
        public BuiltInDataTypesSqlServerFixture()
        {
            _testStore = SqlServerTestStore.CreateScratch();

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

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlServer(_testStore.Connection);

            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                context.Database.EnsureCreated();
            }
        }
Example #10
0
        public void It_creates_unique_query_cache_key()
        {
            using (var testStore = SqlServerTestStore.CreateScratch())
            {
                object     key1, key2;
                Expression query;
                using (var context1 = new QueryKeyCacheContext(rowNumberPaging: true, connection: testStore.Connection))
                {
                    var services = ((IInfrastructure <IServiceProvider>)context1).Instance.GetService <IDbContextServices>().DatabaseProviderServices;
                    query = context1.Set <Poco1>().Skip(4).Take(10).Expression;
                    var generator = services.CompiledQueryCacheKeyGenerator;
                    key1 = generator.GenerateCacheKey(query, false);
                }

                using (var context2 = new QueryKeyCacheContext(rowNumberPaging: false, connection: testStore.Connection))
                {
                    var services  = ((IInfrastructure <IServiceProvider>)context2).Instance.GetService <IDbContextServices>().DatabaseProviderServices;
                    var generator = services.CompiledQueryCacheKeyGenerator;
                    key2 = generator.GenerateCacheKey(query, false);
                }

                Assert.NotEqual(key1, key2);
            }
        }