How to set up the DbContext in xUnit test project properly?
I found a way to do it.
var dbOption = new DbContextOptionsBuilder<MyDbContext>()
.UseSqlServer("....")
.Options;
The George Alexandria's solutions work for me:
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseInMemoryDatabase();
var context = new MyDbContext(optionsBuilder.Options);
The UseInMemoryDatabase
extension method is included in Microsoft.EntityFrameworkCore.InMemory
EF 2.0 requires that all in-memory database are named, so be sure to name it like so:
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseInMemoryDatabase("MyInMemoryDatabseName");
var context = new MyDbContext(optionsBuilder.Options);