本文整理汇总了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();
}
}
示例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);
}
}
示例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);
}
示例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;
}
示例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);
}
示例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