Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Resolve DbContext in WebModule PreInitialize #5218


User avatar
0
hitaspdotnet created

Dear support team Hi, I want run the EF runtime migration method by using MyPluginDbContext.Database.Migrate() command in PreInitialize method of MyPluginMvcWebModule if that migrate isn't applied before. But I can't resolve required services for that. I'm trying to resolve DbContext with DbContextProvider and IocManager.Resolve<IHttpContextAccessor>().HttpContext.RequestServices.GetRequiredService<CMSDbContext>(); Can you guide me please to resolve DbContext services in modules configuration? It's can be helpful for plugins with DbMigration.

Thanks. Best regards.


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    @csbeginner you can't because it is registered to DI in the initialize method. So, why don't you do it in initialize or post initialize ?

  • User Avatar
    0
    hitaspdotnet created

    <cite>ismcagdas: </cite> @csbeginner you can't because it is registered to DI in the initialize method. So, why don't you do it in initialize or post initialize ?

    Hi @ismcagdas Thanks for reply

    My plugin has EF module in separate class library (made by ABP template without module zero) I thought resolving DbContext in PreInitialize is possible when our current module is on top. So, which method is best for this workflow and what's the safe way for resolving the context for applying migration?

    Thanks again

  • User Avatar
    0
    ismcagdas created
    Support Team

    @csbeginner it is better to do it in PostInitialize of your module because all DI registration will be done at that stage. We are doing database seed in AspNet Zero in a similar way <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/60040b0e177bd3f3169dbd62d438d286a7601013/aspnet-core/src/MyCompanyName.AbpZeroTemplate.EntityFrameworkCore/EntityFrameworkCore/AbpZeroTemplateEntityFrameworkCoreModule.cs#L61">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L61</a>.

  • User Avatar
    0
    hitaspdotnet created

    Thank you for helpful replies. I got the solution

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)