本文整理汇总了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);
}
示例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);
}
}
示例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);
}
}
示例4: PostProxy
public PostProxy(DbContext context)
{
_context = (BloggingContext)context;
_stateManager = (LazyStateManager)context.GetService<IStateManager>();
}