Im building a service using OWIN and I want to inject EF core in memory db using UserDbContext(DBOptions)
Startup.cs:
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
// How to Register In memory DB?!
// I get an exception on DbContextOptions< in UserContext
container.Register<DbContext>(() => {
var optionsBuilder = new DbContextOptionsBuilder<UserContext>()
.UseInMemoryDatabase("UserContext");
return new UserContext(optionsBuilder.Options);
});
container.Register<IUserRepository, UserRepository>();
config.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
I got far enough so that I do not get an exception on starting the service. But when I call API I get an exception saying:
The constructor of type UserContext contains the parameter with name 'options' and type DbContextOptions<UserContext> that is not registered. Please ensure DbContextOptions<TUserContext> is registered, or change the constructor of UserContext
UserRepository.cs
public class UserRepository : IUserRepository
{
private readonly UserContext context;
public UserRepository(UserContext context)
{
this.context = context;
}
}
UserContext.cs
public class UserContext : DbContext
{
public UserContext(DbContextOptions<UserContext> options)
: base()
{
}
public DbSet<User> Users { get; set; }
}
So how do you register in ef core memory db, with UserContext
using Simple Injector? It would be super easy to do this using standard .NET Core DI.