本文整理汇总了C#中DbContextOptionsBuilder.UseInMemoryDatabase方法的典型用法代码示例。如果您正苦于以下问题:C# DbContextOptionsBuilder.UseInMemoryDatabase方法的具体用法?C# DbContextOptionsBuilder.UseInMemoryDatabase怎么用?C# DbContextOptionsBuilder.UseInMemoryDatabase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbContextOptionsBuilder
的用法示例。
在下文中一共展示了DbContextOptionsBuilder.UseInMemoryDatabase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContextRegistry
public ContextRegistry()
{
optionsBuilder = new DbContextOptionsBuilder<ProductContext>();
optionsBuilder.UseInMemoryDatabase();
For<IContext>().Use("Create new instance of data context", x => new ProductContext(optionsBuilder.Options));
}
示例2: PreInitialize
public override void PreInitialize()
{
Configuration.UnitOfWork.IsTransactional = false; //EF Core InMemory DB does not support transactions
var services = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase();
var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(
IocManager.IocContainer,
services
);
var builder = new DbContextOptionsBuilder<BloggingDbContext>();
builder.UseInMemoryDatabase()
.UseInternalServiceProvider(serviceProvider);
IocManager.IocContainer.Register(
Component
.For<DbContextOptions<BloggingDbContext>>()
.Instance(builder.Options)
.LifestyleSingleton()
);
Configuration.ReplaceService<IRepository<Post, Guid>>(() =>
{
IocManager.IocContainer.Register(
Component.For<IRepository<Post, Guid>, IPostRepository, PostRepository>()
.ImplementedBy<PostRepository>()
.LifestyleTransient()
);
});
}
示例3: HomeControllerTests
public HomeControllerTests()
{
// Database setup
var services = new ServiceCollection();
services.AddEntityFramework()
.AddSqlServer()
.AddInMemoryDatabase()
.AddDbContext<DataDbContext>(options =>
options.UseInMemoryDatabase()
);
// Dependencies initializations
_pageConfiguration = new FakePageConfiguration();
var optionsBuilder = new DbContextOptionsBuilder<DataDbContext>();
optionsBuilder.UseInMemoryDatabase();
_dataDbContext = new DataDbContext(optionsBuilder.Options);
_contentRepository = new ContentRepository(_dataDbContext);
_humanReadableContentService = new HumanReadableContentService(_pageConfiguration, _contentRepository);
_languageManipulationService = new LanguageManipulationService();
// Controller initialization
_homeController = new PersonalWebsite.Controllers.HomeController(
_pageConfiguration,
_humanReadableContentService,
_languageManipulationService
);
}
示例4: CreateStore
private static IInMemoryDatabase CreateStore(IServiceProvider serviceProvider, bool persist)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase();
return InMemoryTestHelpers.Instance.CreateContextServices(serviceProvider, optionsBuilder.Options).GetRequiredService<IInMemoryDatabase>();
}
示例5: CreateOptions
protected override DbContextOptions CreateOptions(string databaseName)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase();
return optionsBuilder.Options;
}
示例6: GetPendingOrders
public void GetPendingOrders()
{
var builder = new DbContextOptionsBuilder<UnicornStoreContext>();
builder.UseInMemoryDatabase(persist: true);
var options = builder.Options;
using (var context = new UnicornStoreContext(options))
{
var orders = new List<Order>
{
new Order { State = OrderState.CheckingOut, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.Placed, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.Filling, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.ReadyToShip, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.Shipped, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.Delivered, ShippingDetails = new OrderShippingDetails() },
new Order { State = OrderState.Cancelled, ShippingDetails = new OrderShippingDetails() },
};
context.AddRange(orders);
context.AddRange(orders.Select(o => o.ShippingDetails));
context.SaveChanges();
}
using (var context = new UnicornStoreContext(options))
{
var controller = new ShippingController(context);
var orders = controller.PendingOrders();
Assert.Equal(1, orders.Count());
}
}
示例7: Is_configured_when_configuration_contains_associated_extension
public void Is_configured_when_configuration_contains_associated_extension()
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase();
Assert.True(new DatabaseProvider<InMemoryDatabaseProviderServices, InMemoryOptionsExtension>().IsConfigured(optionsBuilder.Options));
}
示例8: Seed
public Seed()
{
_context = new DbContextOptionsBuilder<Context>();
_context.UseInMemoryDatabase();
BrandSeed();
CarSeed();
}
示例9: Can_add_extension_using_persist_true
public void Can_add_extension_using_persist_true()
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase(persist: true);
var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();
Assert.True(extension.Persist);
}
示例10: Can_add_extension_with_connection_string_using_generic_builder
public void Can_add_extension_with_connection_string_using_generic_builder()
{
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseInMemoryDatabase(persist: false);
var extension = (InMemoryOptionsExtension)optionsBuilder.Options.Extensions.Single();
Assert.False(extension.Persist);
}
示例11: GamePlatformRepositoryTests
public GamePlatformRepositoryTests()
{
var db = new DbContextOptionsBuilder<TeammateOnlineContext>();
db.UseInMemoryDatabase();
context = new TeammateOnlineContext(db.Options);
gamePlatformRepository = new GamePlatformRepository(context);
SeedData();
}
示例12: UserProfileRepositoryTests
public UserProfileRepositoryTests()
{
var db = new DbContextOptionsBuilder<TeammateOnlineContext>();
db.UseInMemoryDatabase();
context = new TeammateOnlineContext(db.Options);
userProfileRepository = new UserProfileRepository(context);
SeedData();
}
示例13: Music_store_project_to_mapped_entity
public void Music_store_project_to_mapped_entity()
{
var serviceProvider
= new ServiceCollection()
.AddEntityFramework()
.AddInMemoryDatabase()
.ServiceCollection()
.BuildServiceProvider();
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInMemoryDatabase();
using (var db = new MusicStoreContext(serviceProvider, optionsBuilder.Options))
{
var albums = GetAlbums("~/Images/placeholder.png", Genres, Artists);
db.Genres.AddRange(Genres.Values);
db.Artists.AddRange(Artists.Values);
db.Albums.AddRange(albums);
db.SaveChanges();
}
using (var db = new MusicStoreContext(serviceProvider, optionsBuilder.Options))
{
var q = from album in db.Albums
join genre in db.Genres on album.GenreId equals genre.GenreId
join artist in db.Artists on album.ArtistId equals artist.ArtistId
select new Album
{
ArtistId = album.ArtistId,
AlbumArtUrl = album.AlbumArtUrl,
AlbumId = album.AlbumId,
GenreId = album.GenreId,
Price = album.Price,
Title = album.Title,
Artist = new Artist
{
ArtistId = album.ArtistId,
Name = artist.Name
},
Genre = new Genre
{
GenreId = album.GenreId,
Name = genre.Name
}
};
var albums = q.ToList();
Assert.Equal(462, albums.Count);
}
}
示例14: TestDatabaseStore
/// <summary>
/// Constructor.
/// </summary>
public TestDatabaseStore()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseInMemoryDatabase()
.UseInternalServiceProvider(serviceProvider);
Options = optionsBuilder.Options;
}
示例15: Startup
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
_context = new DbContextOptionsBuilder<Context>();
_context.UseInMemoryDatabase();
}