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


C# ServiceCollection.AddEntityFrameworkInMemoryDatabase方法代码示例

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


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

示例1: TestBase

    public TestBase()
    {
      if (ServiceProvider == null)
      {
        var services = new ServiceCollection();

        // set up empty in-memory test db
        services
          .AddEntityFrameworkInMemoryDatabase()
          .AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase().UseInternalServiceProvider(services.BuildServiceProvider()));

        // add identity service
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<AllReadyContext>();
        var context = new DefaultHttpContext();
        context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
        services.AddSingleton<IHttpContextAccessor>(h => new HttpContextAccessor { HttpContext = context });

        // Setup hosting environment
        IHostingEnvironment hostingEnvironment = new HostingEnvironment();
        hostingEnvironment.EnvironmentName = "Development";
        services.AddSingleton(x => hostingEnvironment);

        // set up service provider for tests
        ServiceProvider = services.BuildServiceProvider();
      }
    }
开发者ID:gftrader,项目名称:allReady,代码行数:27,代码来源:TestBase.cs

示例2: Can_save_and_query_with_explicit_services_and_OnConfiguring

        public void Can_save_and_query_with_explicit_services_and_OnConfiguring()
        {
            var services = new ServiceCollection();
            services.AddEntityFrameworkInMemoryDatabase();
            var serviceProvider = services.BuildServiceProvider();

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

            using (var context = new ExplicitServicesImplicitConfigBlogContext(serviceProvider))
            {
                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:RickyLin,项目名称:EntityFramework,代码行数:25,代码来源:ConfigPatternsInMemoryTest.cs

示例3: Get

 public Get()
 {
     var services = new ServiceCollection();
     services.AddEntityFrameworkInMemoryDatabase()
         .AddDbContext<HeroesContext>(x => x.UseInMemoryDatabase().UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider())); // Don't share context data -> use new InternalServiceProvider per instance
     _context = services.BuildServiceProvider().GetRequiredService<HeroesContext>();
     _controller = new HeroesController(_context);
 }
开发者ID:GeorgDangl,项目名称:NetCoreHeroes,代码行数:8,代码来源:HeroesControllerTests.cs

示例4: CreateContext

        public static InMemoryContext CreateContext()
        {
            var services = new ServiceCollection();
            services.AddEntityFrameworkInMemoryDatabase();
            var serviceProvider = services.BuildServiceProvider();

            var db = new InMemoryContext(new DbContextOptionsBuilder().Options);
            db.Database.EnsureCreated();

            return db;
        }
开发者ID:yonglehou,项目名称:Identity-1,代码行数:11,代码来源:TestIdentityFactory.cs

示例5: SurveyControllerTests

        public SurveyControllerTests()
        {
            _surveyService = new Mock<ISurveyService>();
            _logger = new Mock<ILogger<SurveyController>>();
            _authorizationService = new Mock<IAuthorizationService>();

            var services = new ServiceCollection();
            services.AddEntityFrameworkInMemoryDatabase()
                .AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase());

            _target = new SurveyController(_surveyService.Object, _logger.Object, _authorizationService.Object);
        }
开发者ID:mspnp,项目名称:multitenant-saas-guidance,代码行数:12,代码来源:SurveyControllerTests.cs

示例6: UsesServiceProviderToDiscoverEntities

        public void UsesServiceProviderToDiscoverEntities()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddEntityFrameworkInMemoryDatabase()
                .AddDbContext<DbContext>((sp, x) => x.UseInMemoryDatabase().UseInternalServiceProvider(sp));
            serviceCollection.ConfigureEntityFramework(f => f.Using().AddAlteration<DependingAlteration>());
            serviceCollection.AddSingleton<Dependency>();
            var provider = serviceCollection.BuildServiceProvider();

            var dbContext = provider.GetService<DbContext>();

            var model = dbContext.Model;

            Assert.Equal(typeof(EntityOne), model.GetEntityTypes().First().ClrType);
        }
开发者ID:Grinderofl,项目名称:FluentModelBuilder,代码行数:15,代码来源:BuildingUsingAlterationFromServiceProviderSingleton.cs


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