Beispiel #1
0
        public BuiltInDataTypesInMemoryFixture()
        {
            _testStore = new InMemoryTestStore();
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                  .BuildServiceProvider(validateScopes: true);

            _options = new DbContextOptionsBuilder()
                       .UseInMemoryDatabase(nameof(BuiltInDataTypesInMemoryFixture))
                       .UseInternalServiceProvider(serviceProvider)
                       .Options;
        }
            public FindSqlServerFixture()
            {
                var serviceProvider = new ServiceCollection()
                                      .AddEntityFrameworkSqlServer()
                                      .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                      .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                      .BuildServiceProvider(validateScopes: true);

                _options = new DbContextOptionsBuilder()
                           .UseSqlServer(SqlServerTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                           .UseInternalServiceProvider(serviceProvider)
                           .EnableSensitiveDataLogging()
                           .Options;
            }
Beispiel #3
0
        protected TestStore CreateTestStore(Action <ModelBuilder> onModelCreating)
        {
            TestStore = TestStoreFactory.Create(DatabaseName);

            ServiceProvider = TestStoreFactory.AddProviderServices(new ServiceCollection())
                              .AddSingleton(TestModelSource.GetFactory(onModelCreating))
                              .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                              .BuildServiceProvider(validateScopes: true);

            TestStore.Initialize(ServiceProvider, CreateContext, c => ((TransportationContext)c).Seed());

            TestSqlLoggerFactory.Clear();

            return(TestStore);
        }
 public DataAnnotationInMemoryFixture()
 {
     _options = new DbContextOptionsBuilder()
         .UseInMemoryDatabase(nameof(DataAnnotationInMemoryFixture))
         .UseInternalServiceProvider(new ServiceCollection()
             .AddEntityFrameworkInMemoryDatabase()
             .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
             .BuildServiceProvider())
         .ConfigureWarnings(w =>
             {
                 w.Default(WarningBehavior.Throw);
                 w.Ignore(InMemoryEventId.TransactionIgnoredWarning);
             })
         .Options;
 }
Beispiel #5
0
        public DataAnnotationSqlServerFixture()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkSqlServer()
                                  .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                  .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                  .BuildServiceProvider();

            _options = new DbContextOptionsBuilder()
                       .EnableSensitiveDataLogging()
                       .UseInternalServiceProvider(serviceProvider)
                       .ConfigureWarnings(w =>
            {
                w.Default(WarningBehavior.Throw);
                w.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning);
            }).Options;
        }
Beispiel #6
0
        public BuiltInDataTypesSqliteFixture()
        {
            _testStore = SqliteTestStore.CreateScratch();

            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkSqlite()
                                  .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                  .AddSingleton <ILoggerFactory>(TestSqlLoggerFactory)
                                  .BuildServiceProvider();

            _options = new DbContextOptionsBuilder()
                       .UseSqlite(_testStore.Connection)
                       .EnableSensitiveDataLogging()
                       .UseInternalServiceProvider(serviceProvider)
                       .Options;

            using (var context = new DbContext(_options))
            {
                context.Database.EnsureClean();
            }
        }
Beispiel #7
0
        private ContextFactory <TContext> Initialize <TContext>(
            Action <ModelBuilder> onModelCreating,
            Action <DbContextOptionsBuilder> onConfiguring,
            Action <IServiceCollection> addServices,
            Func <string, bool> shouldLogCategory,
            Func <TestStore> createTestStore,
            bool usePooling)
            where TContext : DbContext
        {
            _testStore = createTestStore?.Invoke() ?? CreateTestStore();

            shouldLogCategory ??= _ => false;
            var services = TestStoreFactory.AddProviderServices(new ServiceCollection())
                           .AddSingleton <ILoggerFactory>(TestStoreFactory.CreateListLoggerFactory(shouldLogCategory));

            if (onModelCreating != null)
            {
                services = services.AddSingleton(TestModelSource.GetFactory(onModelCreating));
            }

            if (addServices != null)
            {
                addServices(services);
            }

            services = usePooling
                ? services.AddDbContextPool(typeof(TContext), (s, b) => ConfigureOptions(s, b, onConfiguring))
                : services.AddDbContext(
                typeof(TContext),
                (s, b) => ConfigureOptions(s, b, onConfiguring),
                ServiceLifetime.Transient,
                ServiceLifetime.Singleton);

            _serviceProvider = services.BuildServiceProvider(validateScopes: true);

            var contextFactory = new ContextFactory <TContext>(_serviceProvider, usePooling, _testStore);

            return(contextFactory);
        }
        public async Task Can_add_update_delete_end_to_end()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .AddSingleton <ILoggerFactory>(new ListLoggerFactory())
                                  .AddSingleton(TestModelSource.GetFactory(OnModelCreating))
                                  .BuildServiceProvider();

            var options = new DbContextOptionsBuilder()
                          .UseInternalServiceProvider(serviceProvider)
                          .UseInMemoryDatabase(nameof(DatabaseInMemoryTest))
                          .Options;

            var customer = new Customer
            {
                Id   = 42,
                Name = "Theon"
            };

            using (var context = new DbContext(options))
            {
                context.Add(customer);

                await context.SaveChangesAsync();

                customer.Name = "Changed!";
            }

            using (var context = new DbContext(options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal("Theon", customerFromStore.Name);
            }

            using (var context = new DbContext(options))
            {
                customer.Name = "Theon Greyjoy";
                context.Update(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal("Theon Greyjoy", customerFromStore.Name);
            }

            using (var context = new DbContext(options))
            {
                context.Remove(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(options))
            {
                Assert.Equal(0, context.Set <Customer>().Count());
            }
        }
 protected virtual IServiceCollection AddServices(IServiceCollection serviceCollection)
 => serviceCollection.AddSingleton(TestModelSource.GetFactory(OnModelCreating));
 private IServiceProvider BuildServiceProvider(Action <ModelBuilder> onModelCreating)
 => new ServiceCollection()
 .AddEntityFrameworkSqlite()
 .AddSingleton(TestModelSource.GetFactory(onModelCreating))
 .BuildServiceProvider();