public void Is_configured_when_configuration_contains_associated_extension()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();

            Assert.True(new InMemoryDataStoreSource().IsConfigured(optionsBuilder.Options));
        }
Exemple #2
0
        public void Can_save_and_query_with_implicit_services_and_explicit_config()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();

            using (var context = new ImplicitServicesExplicitConfigBlogContext(optionsBuilder.Options))
            {
                context.Blogs.Add(new Blog {
                    Name = "The Waffle Cart"
                });
                context.SaveChanges();
            }

            using (var context = new ImplicitServicesExplicitConfigBlogContext(optionsBuilder.Options))
            {
                var blog = context.Blogs.SingleOrDefault();

                Assert.NotEqual(0, blog.Id);
                Assert.Equal("The Waffle Cart", blog.Name);

                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                Assert.Empty(context.Blogs);
            }
        }
Exemple #3
0
        public void Can_save_and_query_with_explicit_services_and_explicit_config()
        {
            var services = new ServiceCollection();

            services.AddEntityFramework().AddInMemoryStore();
            var serviceProvider = services.BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();

            using (var context = new ExplicitServicesAndConfigBlogContext(serviceProvider, optionsBuilder.Options))
            {
                context.Blogs.Add(new Blog {
                    Name = "The Waffle Cart"
                });
                context.SaveChanges();
            }

            using (var context = new ExplicitServicesAndConfigBlogContext(serviceProvider, optionsBuilder.Options))
            {
                var blog = context.Blogs.SingleOrDefault();

                Assert.NotEqual(0, blog.Id);
                Assert.Equal("The Waffle Cart", blog.Name);

                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                Assert.Empty(context.Blogs);
            }
        }
Exemple #4
0
        public void Can_inject_different_configurations_into_different_contexts_without_declaring_in_constructor()
        {
            var blogOptions = new EntityOptionsBuilder <InjectDifferentConfigurationsNoConstructorBlogContext>();

            blogOptions.UseInMemoryStore();

            var accountOptions = new EntityOptionsBuilder <InjectDifferentConfigurationsNoConstructorAccountContext>();

            accountOptions.UseInMemoryStore();

            var services = new ServiceCollection();

            services.AddTransient <InjectDifferentConfigurationsNoConstructorBlogContext>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorAccountContext>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorBlogController>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorAccountController>()
            .AddInstance(blogOptions.Options)
            .AddInstance(accountOptions.Options)
            .AddEntityFramework()
            .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetRequiredService <InjectDifferentConfigurationsNoConstructorBlogController>().Test();
            serviceProvider.GetRequiredService <InjectDifferentConfigurationsNoConstructorAccountController>().Test();
        }
Exemple #5
0
        public void Cannot_inject_different_configurations_into_different_contexts_both_as_base_type_without_constructor()
        {
            var blogOptions = new EntityOptionsBuilder <InjectDifferentConfigurationsNoConstructorBlogContext>();

            blogOptions.UseInMemoryStore();

            var accountOptions = new EntityOptionsBuilder <InjectDifferentConfigurationsNoConstructorAccountContext>();

            accountOptions.UseInMemoryStore();

            var services = new ServiceCollection();

            services.AddTransient <InjectDifferentConfigurationsNoConstructorBlogContext>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorAccountContext>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorBlogController>()
            .AddTransient <InjectDifferentConfigurationsNoConstructorAccountController>()
            .AddInstance <EntityOptions>(blogOptions.Options)
            .AddInstance <EntityOptions>(accountOptions.Options)
            .AddEntityFramework()
            .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            Assert.Equal(
                CoreStrings.NonGenericOptions,
                Assert.Throws <InvalidOperationException>(
                    () => serviceProvider.GetRequiredService <InjectDifferentConfigurationsNoConstructorBlogController>().Test()).Message);
        }
        private static IInMemoryDataStore CreateStore(IServiceProvider serviceProvider, bool persist)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore(persist: persist);

            return(InMemoryTestHelpers.Instance.CreateContextServices(serviceProvider, optionsBuilder.Options).GetRequiredService <IInMemoryDataStore>());
        }
Exemple #7
0
        protected override EntityOptions CreateOptions(string databaseName)
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();

            return(optionsBuilder.Options);
        }
            protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
            {
                Assert.Same(_options, optionsBuilder.Options);

                optionsBuilder.UseInMemoryStore();

                Assert.NotSame(_options, optionsBuilder.Options);
            }
Exemple #9
0
        public void Is_configured_when_configuration_contains_associated_extension()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();

            Assert.True(new InMemoryDataStoreSource().IsConfigured(optionsBuilder.Options));
        }
Exemple #10
0
        private static IServiceProvider CreateContextServices(IModel model)
        {
            var optionsBuilder = new EntityOptionsBuilder()
                                 .UseModel(model);

            optionsBuilder.UseInMemoryStore(persist: false);

            return(((IAccessor <IServiceProvider>) new DbContext(optionsBuilder.Options)).Service);
        }
        public void Can_add_extension_using_persist_true()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: true);

            var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();

            Assert.True(extension.Persist);
        }
        public void Can_add_extension_with_connection_string_using_generic_builder()
        {
            var optionsBuilder = new EntityOptionsBuilder<DbContext>();
            optionsBuilder.UseInMemoryStore(persist: false);

            var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();

            Assert.False(extension.Persist);
        }
Exemple #13
0
            public InMemoryGraphUpdatesTestStore(IServiceProvider serviceProvider)
            {
                var optionsBuilder = new EntityOptionsBuilder();

                optionsBuilder.UseInMemoryStore(persist: true);

                Context = new GraphUpdatesContext(serviceProvider, optionsBuilder.Options);

                Context.Database.EnsureCreated();
            }
        public void Can_add_extension_with_connection_string_using_generic_builder()
        {
            var optionsBuilder = new EntityOptionsBuilder <DbContext>();

            optionsBuilder.UseInMemoryStore(persist: false);

            var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();

            Assert.False(extension.Persist);
        }
            public override DbContext CreateContext(InMemoryTestStore testStore)
            {
                var optionsBuilder = new EntityOptionsBuilder();

                optionsBuilder.UseInMemoryStore();

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

                return(context);
            }
        public void Can_add_extension_using_persist_true()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore(persist: true);

            var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();

            Assert.True(extension.Persist);
        }
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     if (UseSqlServer)
     {
         optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);
     }
     else
     {
         optionsBuilder.UseInMemoryStore();
     }
 }
        public void Music_store_project_to_mapped_entity()
        {
            var serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: true);

            using (var db = new MusicStoreContext(serviceProvider, optionsBuilder.Options))
            {
                var albums = GetAlbums("~/Images/placeholder.png", Genres, Artists);

                db.Genres.AddRange(Genres.Values);
                db.Artists.AddRange(Artists.Values);
                db.Albums.AddRange(albums);

                db.SaveChanges();
            }

            using (var db = new MusicStoreContext(serviceProvider, optionsBuilder.Options))
            {
                var q = from album in db.Albums
                    join genre in db.Genres on album.GenreId equals genre.GenreId
                    join artist in db.Artists on album.ArtistId equals artist.ArtistId
                    select new Album
                        {
                            ArtistId = album.ArtistId,
                            AlbumArtUrl = album.AlbumArtUrl,
                            AlbumId = album.AlbumId,
                            GenreId = album.GenreId,
                            Price = album.Price,
                            Title = album.Title,
                            Artist = new Artist
                                {
                                    ArtistId = album.ArtistId,
                                    Name = artist.Name
                                },
                            Genre = new Genre
                                {
                                    GenreId = album.GenreId,
                                    Name = genre.Name
                                }
                        };

                var albums = q.ToList();

                Assert.Equal(462, albums.Count);
            }
        }
            public override InMemoryTestStore CreateTestStore()
            {
                return(InMemoryTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new EntityOptionsBuilder();
                    optionsBuilder.UseInMemoryStore();

                    using (var context = new StoreGeneratedContext(_serviceProvider, optionsBuilder.Options))
                    {
                        context.Database.EnsureDeleted();
                        context.Database.EnsureCreated();
                    }
                }));
            }
        public BuiltInDataTypesInMemoryFixture()
        {
            _testStore = new InMemoryTestStore();
            _serviceProvider = new ServiceCollection()
                .AddEntityFramework()
                .AddInMemoryStore()
                .ServiceCollection()
                .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;
        }
Exemple #21
0
        public BuiltInDataTypesInMemoryFixture()
        {
            _testStore       = new InMemoryTestStore();
            _serviceProvider = new ServiceCollection()
                               .AddEntityFramework()
                               .AddInMemoryStore()
                               .ServiceCollection()
                               .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                               .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;
        }
Exemple #22
0
            public NullKeysInMemoryFixture()
            {
                _serviceProvider = new ServiceCollection()
                                   .AddEntityFramework()
                                   .AddInMemoryStore()
                                   .ServiceCollection()
                                   .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                                   .BuildServiceProvider();

                var optionsBuilder = new EntityOptionsBuilder();

                optionsBuilder.UseInMemoryStore(persist: true);
                _options = optionsBuilder.Options;

                EnsureCreated();
            }
        public InheritanceInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddInMemoryStore()
                  .ServiceCollection()
                  .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                  .BuildServiceProvider();

            _optionsBuilder.UseInMemoryStore();

            using (var context = CreateContext())
            {
                SeedData(context);
            }
        }
Exemple #24
0
        public void Can_register_configuration_with_DI_container_and_have_it_injected()
        {
            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore(persist: false);

            var services = new ServiceCollection();

            services.AddTransient <InjectConfigurationBlogContext>()
            .AddTransient <InjectConfigurationController>()
            .AddInstance(optionsBuilder.Options)
            .AddEntityFramework()
            .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetRequiredService <InjectConfigurationController>().Test();
        }
        public NorthwindQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = CreateContext())
            {
                NorthwindData.Seed(context);
            }
        }
        public OneToOneQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                    .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                AddTestData(context);
            }
        }
Exemple #27
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();
        }
Exemple #28
0
        public NorthwindQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddInMemoryStore()
                  .ServiceCollection()
                  .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                  .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = CreateContext())
            {
                NorthwindData.Seed(context);
            }
        }
        public OneToOneQueryInMemoryFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddInMemoryStore()
                  .ServiceCollection()
                  .AddSingleton(TestInMemoryModelSource.GetFactory(OnModelCreating))
                  .BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();

            optionsBuilder.UseInMemoryStore();
            _options = optionsBuilder.Options;

            using (var context = new DbContext(_serviceProvider, _options))
            {
                AddTestData(context);
            }
        }
        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();
        }
        public void Can_save_and_query_with_implicit_services_and_explicit_config()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();

            using (var context = new ImplicitServicesExplicitConfigBlogContext(optionsBuilder.Options))
            {
                context.Blogs.Add(new Blog { Name = "The Waffle Cart" });
                context.SaveChanges();
            }

            using (var context = new ImplicitServicesExplicitConfigBlogContext(optionsBuilder.Options))
            {
                var blog = context.Blogs.SingleOrDefault();

                Assert.NotEqual(0, blog.Id);
                Assert.Equal("The Waffle Cart", blog.Name);

                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                Assert.Empty(context.Blogs);
            }
        }
        public void Can_save_and_query_with_explicit_services_and_explicit_config()
        {
            var services = new ServiceCollection();
            services.AddEntityFramework().AddInMemoryStore();
            var serviceProvider = services.BuildServiceProvider();

            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore();

            using (var context = new ExplicitServicesAndConfigBlogContext(serviceProvider, optionsBuilder.Options))
            {
                context.Blogs.Add(new Blog { Name = "The Waffle Cart" });
                context.SaveChanges();
            }

            using (var context = new ExplicitServicesAndConfigBlogContext(serviceProvider, optionsBuilder.Options))
            {
                var blog = context.Blogs.SingleOrDefault();

                Assert.NotEqual(0, blog.Id);
                Assert.Equal("The Waffle Cart", blog.Name);

                context.Blogs.RemoveRange(context.Blogs);
                context.SaveChanges();

                Assert.Empty(context.Blogs);
            }
        }
        public override void AutoConfigure(EntityOptionsBuilder optionsBuilder)
        {
            Check.NotNull(optionsBuilder, nameof(optionsBuilder));

            optionsBuilder.UseInMemoryStore();
        }
Exemple #34
0
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseInMemoryStore();
 }
 protected internal override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseInMemoryStore(persist: false);
 }
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseInMemoryStore();
 }
        public void Can_inject_different_configurations_into_different_contexts_without_declaring_in_constructor()
        {
            var blogOptions = new EntityOptionsBuilder<InjectDifferentConfigurationsNoConstructorBlogContext>();
            blogOptions.UseInMemoryStore();

            var accountOptions = new EntityOptionsBuilder<InjectDifferentConfigurationsNoConstructorAccountContext>();
            accountOptions.UseInMemoryStore();

            var services = new ServiceCollection();
            services.AddTransient<InjectDifferentConfigurationsNoConstructorBlogContext>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorAccountContext>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorBlogController>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorAccountController>()
                .AddInstance(blogOptions.Options)
                .AddInstance(accountOptions.Options)
                .AddEntityFramework()
                .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetRequiredService<InjectDifferentConfigurationsNoConstructorBlogController>().Test();
            serviceProvider.GetRequiredService<InjectDifferentConfigurationsNoConstructorAccountController>().Test();
        }
            protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
            {
                Assert.Same(_options, optionsBuilder.Options);

                optionsBuilder.UseInMemoryStore();

                Assert.NotSame(_options, optionsBuilder.Options);
            }
Exemple #39
0
        public async Task Can_add_update_delete_end_to_end_using_only_shadow_state()
        {
            var model = new Model();

            var customerType = model.AddEntityType("Customer");

            customerType.GetOrSetPrimaryKey(customerType.AddProperty("Id", typeof(int), shadowProperty: true));
            customerType.GetOrAddProperty("Name", typeof(string), shadowProperty: true);

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

            optionsBuilder.UseInMemoryStore();

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                // TODO: Better API for shadow state access
                var customerEntry = ((IAccessor <IStateManager>)context.ChangeTracker).Service.CreateNewEntry(customerType);
                customerEntry[customerType.GetProperty("Id")]   = 42;
                customerEntry[customerType.GetProperty("Name")] = "Daenerys";

                customerEntry.SetEntityState(EntityState.Added);

                await context.SaveChangesAsync();

                customerEntry[customerType.GetProperty("Name")] = "Changed!";
            }

            // TODO: Fix this when we can query shadow entities
            // var customerFromStore = await inMemoryDataStore.Read(customerType).SingleAsync();
            //
            // Assert.Equal(new object[] { 42, "Daenerys" }, customerFromStore);

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var customerEntry = ((IAccessor <IStateManager>)context.ChangeTracker).Service.CreateNewEntry(customerType);
                customerEntry[customerType.GetProperty("Id")]   = 42;
                customerEntry[customerType.GetProperty("Name")] = "Daenerys Targaryen";

                customerEntry.SetEntityState(EntityState.Modified);

                await context.SaveChangesAsync();
            }

            // TODO: Fix this when we can query shadow entities
            // customerFromStore = await inMemoryDataStore.Read(customerType).SingleAsync();
            //
            // Assert.Equal(new object[] { 42, "Daenerys Targaryen" }, customerFromStore);

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var customerEntry = ((IAccessor <IStateManager>)context.ChangeTracker).Service.CreateNewEntry(customerType);
                customerEntry[customerType.GetProperty("Id")] = 42;

                customerEntry.SetEntityState(EntityState.Deleted);

                await context.SaveChangesAsync();
            }

            // TODO: Fix this when we can query shadow entities
            // Assert.Equal(0, await inMemoryDataStore.Read(customerType).CountAsync());
        }
 protected override void OnConfiguring(EntityOptionsBuilder options)
 {
     options.UseInMemoryStore(persist: true);
 }
Exemple #41
0
        public async Task Can_add_update_delete_end_to_end_using_partial_shadow_state()
        {
            var model = new Model();

            var customerType = model.AddEntityType(typeof(Customer));

            customerType.GetOrSetPrimaryKey(customerType.GetOrAddProperty("Id", typeof(int)));
            customerType.GetOrAddProperty("Name", typeof(string), shadowProperty: true);

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

            optionsBuilder.UseInMemoryStore();

            var customer = new Customer {
                Id = 42
            };

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                context.Add(customer);

                // TODO: Better API for shadow state access
                var customerEntry = ((IAccessor <InternalEntityEntry>)context.Entry(customer)).Service;
                customerEntry[customerType.GetProperty("Name")] = "Daenerys";

                await context.SaveChangesAsync();

                customerEntry[customerType.GetProperty("Name")] = "Changed!";
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal(
                    "Daenerys",
                    (string)context.Entry(customerFromStore).Property("Name").CurrentValue);
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var customerEntry = ((IAccessor <InternalEntityEntry>)context.Entry(customer)).Service;
                customerEntry[customerType.GetProperty("Name")] = "Daenerys Targaryen";

                context.Update(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var customerFromStore = context.Set <Customer>().Single();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal(
                    "Daenerys Targaryen",
                    (string)context.Entry(customerFromStore).Property("Name").CurrentValue);
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                context.Remove(customer);

                await context.SaveChangesAsync();
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                Assert.Equal(0, context.Set <Customer>().Count());
            }
        }
Exemple #42
0
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model      = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(object));

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

            optionsBuilder.UseInMemoryStore(persist: false);

            var contextServices = ((IAccessor <IServiceProvider>) new DbContext(optionsBuilder.Options)).Service;
            var stateManager    = contextServices.GetRequiredService <IStateManager>();

            var key = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);

            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());

            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());

            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()), null));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                            new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                            new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
 protected override void OnConfiguring(EntityOptionsBuilder options)
 {
     options.UseInMemoryStore(persist: true);
 }
Exemple #44
0
 protected internal override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseInMemoryStore(persist: false);
 }
        public void Can_register_configuration_with_DI_container_and_have_it_injected()
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: false);

            var services = new ServiceCollection();
            services.AddTransient<InjectConfigurationBlogContext>()
                .AddTransient<InjectConfigurationController>()
                .AddInstance(optionsBuilder.Options)
                .AddEntityFramework()
                .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetRequiredService<InjectConfigurationController>().Test();
        }
 protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
 {
     if (UseSqlServer)
     {
         optionsBuilder.UseSqlServer(SqlServerNorthwindContext.ConnectionString);
     }
     else
     {
         optionsBuilder.UseInMemoryStore();
     }
 }
        private void Can_add_update_delete_end_to_end <T>()
            where T : class
        {
            var type  = typeof(T);
            var model = new Model();

            var entityType   = model.AddEntityType(type);
            var idProperty   = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            var nameProperty = entityType.GetOrAddProperty("Name", typeof(string), shadowProperty: true);

            entityType.GetOrSetPrimaryKey(idProperty);

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

            optionsBuilder.UseInMemoryStore();

            T entity;

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var entry = ((IAccessor <IStateManager>)context.ChangeTracker).Service.CreateNewEntry(entityType);
                entity = (T)entry.Entity;

                entry[idProperty]   = 42;
                entry[nameProperty] = "The";

                entry.SetEntityState(EntityState.Added);

                context.SaveChanges();
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var entityFromStore = context.Set <T>().Single();
                var entityEntry     = context.Entry(entityFromStore);

                Assert.NotSame(entity, entityFromStore);
                Assert.Equal(42, entityEntry.Property(idProperty.Name).CurrentValue);
                Assert.Equal("The", entityEntry.Property(nameProperty.Name).CurrentValue);

                ((IAccessor <InternalEntityEntry>)entityEntry).Service[nameProperty] = "A";

                context.Update(entityFromStore);

                context.SaveChanges();
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                var entityFromStore = context.Set <T>().Single();
                var entry           = context.Entry(entityFromStore);

                Assert.Equal("A", entry.Property(nameProperty.Name).CurrentValue);

                context.Remove(entityFromStore);

                context.SaveChanges();
            }

            using (var context = new DbContext(_fixture.ServiceProvider, optionsBuilder.Options))
            {
                Assert.Equal(0, context.Set <T>().Count());
            }
        }
        public void Cannot_inject_different_configurations_into_different_contexts_both_as_base_type_without_constructor()
        {
            var blogOptions = new EntityOptionsBuilder<InjectDifferentConfigurationsNoConstructorBlogContext>();
            blogOptions.UseInMemoryStore();

            var accountOptions = new EntityOptionsBuilder<InjectDifferentConfigurationsNoConstructorAccountContext>();
            accountOptions.UseInMemoryStore();

            var services = new ServiceCollection();
            services.AddTransient<InjectDifferentConfigurationsNoConstructorBlogContext>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorAccountContext>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorBlogController>()
                .AddTransient<InjectDifferentConfigurationsNoConstructorAccountController>()
                .AddInstance<EntityOptions>(blogOptions.Options)
                .AddInstance<EntityOptions>(accountOptions.Options)
                .AddEntityFramework()
                .AddInMemoryStore();

            var serviceProvider = services.BuildServiceProvider();

            Assert.Equal(
                CoreStrings.NonGenericOptions,
                Assert.Throws<InvalidOperationException>(
                    () => serviceProvider.GetRequiredService<InjectDifferentConfigurationsNoConstructorBlogController>().Test()).Message);
        }
        private static IServiceProvider CreateContextServices(IModel model)
        {
            var optionsBuilder = new EntityOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryStore(persist: false);

            return ((IAccessor<IServiceProvider>)new DbContext(optionsBuilder.Options)).Service;
        }
        private static IInMemoryDataStore CreateStore(IServiceProvider serviceProvider, bool persist)
        {
            var optionsBuilder = new EntityOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: persist);

            return InMemoryTestHelpers.Instance.CreateContextServices(serviceProvider, optionsBuilder.Options).GetRequiredService<IInMemoryDataStore>();
        }
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model = new Entity.Metadata.Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new EntityOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryStore(persist: false);

            var contextServices = ((IAccessor<IServiceProvider>)new DbContext(optionsBuilder.Options)).Service;
            var stateManager = contextServices.GetRequiredService<IStateManager>();

            var key = entityType.GetOrAddProperty("Id", typeof(int), shadowProperty: true);
            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());
            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());
            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());
            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());
            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());
            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory());
            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()), null));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "foo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("B", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory()),
                new ModificationCommand("A", null, new ParameterNameGenerator(), p => p.Relational(), new UntypedValueBufferFactoryFactory())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }