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


C# ServiceCollection.AddDbContext方法代码示例

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


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

示例1: ManageControllerTest

        public ManageControllerTest()
        {
            var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            var services = new ServiceCollection();
            services.AddOptions();
            services
                .AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase().UseInternalServiceProvider(efServiceProvider));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<MusicStoreContext>();

            services.AddLogging();
            services.AddOptions();

            // IHttpContextAccessor is required for SignInManager, and UserManager
            var context = new DefaultHttpContext();
            context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = new TestAuthHandler() });
            services.AddSingleton<IHttpContextAccessor>(
                new HttpContextAccessor()
                    {
                        HttpContext = context,
                    });

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:Cream2015,项目名称:MusicStore,代码行数:26,代码来源:ManageControllerTest.cs

示例2: FancierControllerTests

        public FancierControllerTests(ITestOutputHelper output)
        {
            _output = output;
            _services = new ServiceCollection();
            _services.AddDbContext<WeatherContext>(options => options.UseInMemoryDatabase());


        }
开发者ID:julielerman,项目名称:EFCore-ASPNetCore-WebAPI-RTM,代码行数:8,代码来源:FancierControllersTests.cs

示例3: CheckoutControllerTest

        public CheckoutControllerTest()
        {
            var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            var services = new ServiceCollection();

            services.AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase().UseInternalServiceProvider(efServiceProvider));

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:junxy,项目名称:MusicStore,代码行数:10,代码来源:CheckoutControllerTest.cs

示例4: ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne

        public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne()
        {
            var services = new ServiceCollection();

            services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());

            services.ReplaceDbContext();

            this.AssertCorrectDbContextAndOptions(services);
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:10,代码来源:ServicesTests.cs

示例5: ConfigureFrameworkServices

        private static void ConfigureFrameworkServices(IContainer container)
        {
            var services = new ServiceCollection();
            services.AddEntityFrameworkSqlServer();

            services.AddDbContext<ProviderDbContext>(op =>
               op.UseSqlServer(@"Data Source=.\SQL2016;Initial Catalog=POC_DDD;Integrated Security=False;User ID=srvelicheti;[email protected];MultipleActiveResultSets=true;Trusted_Connection=true;")
               );

            container.Populate(services);
        }
开发者ID:srivelicheti,项目名称:learning-DDD,代码行数:11,代码来源:IocBootStrapper.cs

示例6: ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne

        public void ReplaceDbContextShouldReplaceNonInMemoryDatabaseWithInMemoryScopedOne()
        {
            var services = new ServiceCollection();

            services.AddDbContext<CustomDbContext>(options =>
                options.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=TestDb;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30;"));

            services.ReplaceDbContext();

            this.AssertCorrectDbContextAndOptions(services);
        }
开发者ID:Cream2015,项目名称:MyTested.AspNetCore.Mvc,代码行数:11,代码来源:ServicesTests.cs

示例7: InitializeServices

        private void InitializeServices()
        {
            const string ConnectionString = @"server=(localdb)\MSSQLLocalDb;database=Books;trusted_connection=true";
           
            var services = new ServiceCollection();
            services.AddTransient<BooksService>();
           
            services.AddDbContext<BooksContext>(options =>
                options.UseSqlServer(ConnectionString));
          

            Container = services.BuildServiceProvider();
        }
开发者ID:ProfessionalCSharp,项目名称:ProfessionalCSharp6,代码行数:13,代码来源:Program.cs

示例8: AllReadyDataAccessEF7Tests

        public AllReadyDataAccessEF7Tests()
        {
            if (_serviceProvider == null)
            {
                var services = new ServiceCollection();

                // Add EF (Full DB, not In-Memory)
                services.AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase());

                // Setup hosting environment
                IHostingEnvironment hostingEnvironment = new HostingEnvironment();
                hostingEnvironment.EnvironmentName = "Development";
                services.AddSingleton(x => hostingEnvironment);
                _serviceProvider = services.BuildServiceProvider();
            }
        }
开发者ID:shawnwildermuth,项目名称:allReady,代码行数:16,代码来源:AllReadyDataAccessEF7Tests.cs

示例9: WishDaysControllerTests

        public WishDaysControllerTests()
        {
            var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            var services = new ServiceCollection();

            services.AddDbContext<MyWishesDbContext>(b => b.UseInMemoryDatabase().UseInternalServiceProvider(efServiceProvider));

            services.AddLogging();
            services.AddTransient<IRepository<AppTenant>, DbRepository<AppTenant, string>>();
            services.AddTransient<IRepository<WishDay>, DbRepository<WishDay, int>>();
            services.AddTransient<IRepository<WishItem>, DbRepository<WishItem, int>>();
            services.AddTransient<IRepository<WishItemTag>, DbRepository<WishItemTag, int>>();
            services.AddTransient<IUnitOfWorkContext, MyWishesUnitOfWorkDbContext>();

            services.AddTransient<IUnitOfWorkContext, MyWishesUnitOfWorkDbContext>();
            services.AddTransient<IWishDaysService, WishDaysService>();

            _serviceProvider = services.BuildServiceProvider();
        }
开发者ID:SergiyTrygub,项目名称:mynotes,代码行数:20,代码来源:WishDaysControllerTests.cs


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