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


C# DbContext.GetService方法代码示例

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


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

示例1: It_disposes_scope

        public void It_disposes_scope()
        {
            var fakeServiceProvider = new FakeServiceProvider();
            var context = new DbContext(fakeServiceProvider, new DbContextOptions<DbContext>());

            var scopeService = Assert.IsType<FakeServiceProvider.FakeServiceScope>(context.GetService<IServiceScopeFactory>().CreateScope());

            Assert.False(scopeService.Disposed);

            context.Dispose();

            Assert.True(scopeService.Disposed);

            Assert.Throws<ObjectDisposedException>(() => ((IInfrastructure<IServiceProvider>)context).Instance);
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:15,代码来源:DbContextTest.cs

示例2: SaveChanges_calls_DetectChanges

        public void SaveChanges_calls_DetectChanges()
        {
            var services = new ServiceCollection()
                .AddScoped<IStateManager, FakeStateManager>()
                .AddScoped<IChangeDetector, FakeChangeDetector>();

            var serviceProvider = TestHelpers.Instance.CreateServiceProvider(services);

            using (var context = new DbContext(serviceProvider, new DbContextOptionsBuilder().Options))
            {
                var changeDetector = (FakeChangeDetector)context.GetService<IChangeDetector>();

                Assert.False(changeDetector.DetectChangesCalled);

                context.SaveChanges();

                Assert.True(changeDetector.DetectChangesCalled);
            }
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:19,代码来源:DbContextTest.cs

示例3: SaveChangesAsync_calls_state_manager_SaveChangesAsync

        public async Task SaveChangesAsync_calls_state_manager_SaveChangesAsync()
        {
            var services = new ServiceCollection()
                .AddScoped<IStateManager, FakeStateManager>()
                .AddScoped<IChangeDetector, FakeChangeDetector>();

            var serviceProvider = TestHelpers.Instance.CreateServiceProvider(services);

            using (var context = new DbContext(serviceProvider, new DbContextOptionsBuilder().Options))
            {
                context.ChangeTracker.AutoDetectChangesEnabled = false;

                var stateManager = (FakeStateManager)context.GetService<IStateManager>();

                var entryMock = CreateInternalEntryMock();
                entryMock.Setup(m => m.EntityState).Returns(EntityState.Modified);
                stateManager.InternalEntries = new[] { entryMock.Object };

                Assert.False(stateManager.SaveChangesAsyncCalled);

                await context.SaveChangesAsync();

                Assert.True(stateManager.SaveChangesAsyncCalled);
            }
        }
开发者ID:adwardliu,项目名称:EntityFramework,代码行数:25,代码来源:DbContextTest.cs

示例4: PostProxy

 public PostProxy(DbContext context)
 {
     _context = (BloggingContext)context;
     _stateManager = (LazyStateManager)context.GetService<IStateManager>();
 }
开发者ID:nefremov,项目名称:LazyEntityFramework,代码行数:5,代码来源:PostProxy.cs


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