当前位置: 首页>>代码示例>>C#>>正文


C# DbContextOptionsBuilder.UseInMemoryStore方法代码示例

本文整理汇总了C#中DbContextOptionsBuilder.UseInMemoryStore方法的典型用法代码示例。如果您正苦于以下问题:C# DbContextOptionsBuilder.UseInMemoryStore方法的具体用法?C# DbContextOptionsBuilder.UseInMemoryStore怎么用?C# DbContextOptionsBuilder.UseInMemoryStore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DbContextOptionsBuilder的用法示例。


在下文中一共展示了DbContextOptionsBuilder.UseInMemoryStore方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Is_configured_when_configuration_contains_associated_extension

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

            Assert.True(new InMemoryDataStoreSource().IsConfigured(optionsBuilder.Options));
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:InMemoryDataStoreSourceTest.cs

示例2: CreateOptions

        protected override DbContextOptions CreateOptions(string databaseName)
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseInMemoryStore();

            return optionsBuilder.Options;
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:MonsterFixupInMemoryTest.cs

示例3: CreateContext

        public override CrossStoreContext CreateContext(TestStore testStore)
        {
            var inMemoryTestStore = testStore as InMemoryTestStore;
            if (inMemoryTestStore != null)
            {
                var optionsBuilder = new DbContextOptionsBuilder();
                optionsBuilder.UseInMemoryStore();

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

            var sqlServerTestStore = testStore as SqlServerTestStore;
            if (sqlServerTestStore != null)
            {
                var optionsBuilder = new DbContextOptionsBuilder();
                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();
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:26,代码来源:SharedCrossStoreFixture.cs

示例4: Can_add_extension_using_persist_true

        public void Can_add_extension_using_persist_true()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: true);

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

            Assert.True(extension.Persist);
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:9,代码来源:InMemoryDbContextOptionsExtensionsTest.cs

示例5: Can_add_extension_with_connection_string_using_generic_builder

        public void Can_add_extension_with_connection_string_using_generic_builder()
        {
            var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
            optionsBuilder.UseInMemoryStore(persist: false);

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

            Assert.False(extension.Persist);
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:9,代码来源:InMemoryDbContextOptionsExtensionsTest.cs

示例6: Music_store_project_to_mapped_entity

        public void Music_store_project_to_mapped_entity()
        {
            var serviceProvider
                = new ServiceCollection()
                    .AddEntityFramework()
                    .AddInMemoryStore()
                    .ServiceCollection()
                    .BuildServiceProvider();

            var optionsBuilder = new DbContextOptionsBuilder();
            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);
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:53,代码来源:MusicStoreQueryTests.cs

示例7: NorthwindQueryInMemoryFixture

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

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

            using (var context = CreateContext())
            {
                NorthwindData.Seed(context);
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:19,代码来源:NorthwindQueryInMemoryFixture.cs

示例8: Can_save_and_query_with_implicit_services_and_explicit_config

        public void Can_save_and_query_with_implicit_services_and_explicit_config()
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            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);
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:24,代码来源:ConfigPatternsInMemoryTest.cs

示例9: Can_inject_different_configurations_into_different_contexts_without_declaring_in_constructor

        public void Can_inject_different_configurations_into_different_contexts_without_declaring_in_constructor()
        {
            var blogOptions = new DbContextOptionsBuilder<InjectDifferentConfigurationsNoConstructorBlogContext>();
            blogOptions.UseInMemoryStore();

            var accountOptions = new DbContextOptionsBuilder<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();
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:23,代码来源:ConfigPatternsInMemoryTest.cs

示例10: Compare_returns_0_only_for_commands_that_are_equal

        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 DbContextOptionsBuilder()
                .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 BoxedValueReaderSource());
            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 BoxedValueReaderSource());
            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 BoxedValueReaderSource());
            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 BoxedValueReaderSource()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator(), p => p.Relational(), new BoxedValueReaderSource())));

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

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

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

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

            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));
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:74,代码来源:ModificationCommandComparerTest.cs

示例11: OnConfiguring

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     optionsBuilder.UseInMemoryStore();
 }
开发者ID:thegido,项目名称:EntityFramework,代码行数:4,代码来源:SqlServerConfigPatternsTest.cs

示例12: FixupTest

        private static void FixupTest(IModel model)
        {
            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryStore(persist: false);

            using (var context = new DbContext(optionsBuilder.Options))
            {
                var guid1 = Guid.NewGuid();
                var guid2 = Guid.NewGuid();
                var guid3 = Guid.NewGuid();

                context.Add(new KoolEntity1 { Id1 = 11, Id2 = guid1, KoolEntity2Id = 24 });
                context.Add(new KoolEntity1 { Id1 = 12, Id2 = guid2, KoolEntity2Id = 24 });
                context.Add(new KoolEntity1 { Id1 = 13, Id2 = guid3, KoolEntity2Id = 25 });

                context.Add(new KoolEntity2 { Id = 21, KoolEntity1Id1 = 11, KoolEntity1Id2 = guid1, KoolEntity3Id = 33 });
                context.Add(new KoolEntity2 { Id = 22, KoolEntity1Id1 = 11, KoolEntity1Id2 = guid1, KoolEntity3Id = 33 });
                context.Add(new KoolEntity2 { Id = 23, KoolEntity1Id1 = 11, KoolEntity1Id2 = guid1, KoolEntity3Id = 35 });
                context.Add(new KoolEntity2 { Id = 24, KoolEntity1Id1 = 12, KoolEntity1Id2 = guid2, KoolEntity3Id = 35 });
                context.Add(new KoolEntity2 { Id = 25, KoolEntity1Id1 = 12, KoolEntity1Id2 = guid2, KoolEntity3Id = 35 });

                context.Add(new KoolEntity3 { Id = 31 });
                context.Add(new KoolEntity3 { Id = 32 });
                context.Add(new KoolEntity3 { Id = 33 });
                context.Add(new KoolEntity3 { Id = 34 });
                context.Add(new KoolEntity3 { Id = 35 });

                Assert.Equal(3, context.ChangeTracker.Entries<KoolEntity1>().Count());
                Assert.Equal(5, context.ChangeTracker.Entries<KoolEntity2>().Count());
                Assert.Equal(5, context.ChangeTracker.Entries<KoolEntity3>().Count());

                foreach (var entry in context.ChangeTracker.Entries<KoolEntity1>())
                {
                    var entity = entry.Entity;

                    Assert.Equal(entity.KoolEntity2Id, entity.NavTo2.Id);
                    Assert.Contains(entity, entity.NavTo2.NavTo1s);
                }

                foreach (var entry in context.ChangeTracker.Entries<KoolEntity2>())
                {
                    var entity = entry.Entity;

                    // TODO: This broke after removing IForeignKey.ReferencingProperties
                    //Assert.Equal(entity.KoolEntity1Id1, entity.NavTo1.Id1);
                    //Assert.Equal(entity.KoolEntity1Id2, entity.NavTo1.Id2);
                    //Assert.Contains(entity, entity.NavTo1.NavTo2s);
                }
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:51,代码来源:CompiledModelTest.cs

示例13: Navigation_fixup_happens_with_compiled_metadata_using_non_standard_collection_access

        public void Navigation_fixup_happens_with_compiled_metadata_using_non_standard_collection_access()
        {
            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(new _OneTwoThreeContextModel());
            optionsBuilder.UseInMemoryStore();

            using (var context = new DbContext(optionsBuilder.Options))
            {
                context.Add(new KoolEntity6 { Id = 11, Kool5Id = 24 });
                context.Add(new KoolEntity5 { Id = 21 });
                context.Add(new KoolEntity6 { Id = 12, Kool5Id = 24 });
                context.Add(new KoolEntity5 { Id = 22 });
                context.Add(new KoolEntity5 { Id = 23 });
                context.Add(new KoolEntity6 { Id = 13, Kool5Id = 25 });
                context.Add(new KoolEntity5 { Id = 24 });
                context.Add(new KoolEntity5 { Id = 25 });

                Assert.Equal(3, context.ChangeTracker.Entries<KoolEntity6>().Count());
                Assert.Equal(5, context.ChangeTracker.Entries<KoolEntity5>().Count());

                foreach (var entry in context.ChangeTracker.Entries<KoolEntity6>())
                {
                    var entity = entry.Entity;

                    Assert.Equal(entity.Kool5Id, entity.Kool5.Id);
                    Assert.Contains(entity, entity.Kool5.Kool6s);
                }
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:29,代码来源:CompiledModelTest.cs

示例14: CreateContextServices

        private static IServiceProvider CreateContextServices(IModel model)
        {
            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryStore(persist: false);

            return ((IAccessor<IServiceProvider>)new DbContext(optionsBuilder.Options)).Service;
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:8,代码来源:CommandBatchPreparerTest.cs

示例15: CreateStore

        private static IInMemoryDataStore CreateStore(IServiceProvider serviceProvider, bool persist)
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseInMemoryStore(persist: persist);

            return InMemoryTestHelpers.Instance.CreateContextServices(serviceProvider, optionsBuilder.Options).GetRequiredService<IInMemoryDataStore>();
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:7,代码来源:InMemoryDataStoreCreatorTest.cs


注:本文中的DbContextOptionsBuilder.UseInMemoryStore方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。