Skip to content
No description or website provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Uses .Net Core v2.2

I'm not sure the best way to initalise the Jasper host in my unit tests. The problem I'm encountering is that my Db Context is not being correctly initialised with the Provider name unless I initialise the Jasper Host with a custom "TestStartup" class.

If I don't use a test specific startup.cs class, and create a new service collection in my unit test, add the DbContext and call BuildServiceProvider(), any DbContext accessed from the Lamar container throws this error:

System.InvalidOperationException : No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

I've tried explicitly registering the DbContext in the Lamar Container, but no joy:

var opt = new DbContextOptionsBuilder<AppDbContext>()

registry.For<AppDbContext>().Use(t => new AppDbContext(opt));

I guess I'm looking for the best practice steps for this:

  1. Init Jasper Host
  2. Create Service Collection
  3. Build Service Provider
  4. Add DbContext (with options, incl service provider??)
  5. Start invoking messages on the command bus

Should I need to register my test DbContext in the container so that Jasper can get the right one when it invokes my command/query handlers?


Is it sufficent and suitable to provide a basic TestStartup.cs class that calls service.AddDbContext<AppDbContext>(...)??

You can’t perform that action at this time.