jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
326 stars 90 forks source link

Error when registering dbcontext implementation #112

Closed raminmjj closed 2 years ago

raminmjj commented 2 years ago

Hi, I created an application based on the "Clean Architecture Solution Template". and add another database provider like this article. everything works fine when the provider is set to base dbcontext, but when I change provider to another, I got these errors: image

image

the second dbcontext registration is like this: image

Thank you.

rezabashiri commented 2 years ago

Same as me at jbogard/MediatR#682

jbogard commented 2 years ago

This is not an issue with MediatR or how MediatR is registered with the container. You're trying to use a scoped service (DbContext) outside of a scope. You need to create a scope, then resolve services from that scope. See this example.

raminmjj commented 2 years ago

Hi, jimmy. you're right, the problem was in the base dbcontext constructor. I changed it from: image

to

image

Thank you.