本文整理汇总了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();
}
示例2: FancierControllerTests
public FancierControllerTests(ITestOutputHelper output)
{
_output = output;
_services = new ServiceCollection();
_services.AddDbContext<WeatherContext>(options => options.UseInMemoryDatabase());
}
示例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();
}
示例4: ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne
public void ReplaceDbContextShouldReplaceInMemoryDatabaseWithInMemoryScopedOne()
{
var services = new ServiceCollection();
services.AddDbContext<CustomDbContext>(options => options.UseInMemoryDatabase());
services.ReplaceDbContext();
this.AssertCorrectDbContextAndOptions(services);
}
示例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);
}
示例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);
}
示例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();
}
示例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();
}
}
示例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();
}