Open Closed

unable-to-create-migration-on-Module #8437


User avatar
0
Jurjen created
  • ABP Framework version: v9.0.0
  • UI Type: Blazor WASM / Blazor Server
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

I want to create a migration for the tables in my Module, I'm doing this from the PackageManager console I've selected modules{ModuleName}.EntityFrameworkCore project as 'Default project' Then I execute the following:

add-migration -name "Initial" -context {ModuleName}DbContext

which results in:

Build started... Build succeeded. File 'E:\Gits.Git.Projects\Idas\VITRUVIUS-ABP900\src{Application}.Blazor\bin\Debug\net9.0{ModuleName}.EntityFrameworkCore.dll' not found.

I found an article online indicating that perhaps the package Microsoft.EntityFrameworkCore.Tools is missing, installed that in my {ModuleName}.EntityFrameworkCore.

I set {ModuleName}.EntityFrameworkCore as startup project, and the again doing the same add-migration I now get

add-migration -name "Initial" -context {ModuleName}DbContext

which then results in: Build started... Build succeeded. Unable to create a 'DbContext' of type '{ModuleName}DbContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[{ModuleName}.EntityFrameworkCore.{ModuleName}DbContext]' while attempting to activate '{ModuleName}.EntityFrameworkCore.VitruviusBaseDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 Name

I'm not sure how to proceed. Perhaps you can point me in the right direction as to how to work with migrations for a module ?

Kind regards, Jurjen.


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    1. Please share your solution structure.
    2. Please share the structure of the EF core project files.

    Thanks,

  • User Avatar
    0
    Jurjen created

    Hi,

    Took some time but at this time I can make migrations in my module, not really sure why. I suppose the code I initially added wasn't complete.

    However the generated migration doesn't compile

    it seems the SqlServerModelBuilderExtensions don't get recognised (see picture), it needs the package Microsoft.EntityFrameworkCore.SqlServer which doesn't seem to be installed in the generated module-project {ModuleName}.EntityFrameworkCore

    After installing:

    install-package Microsoft.EntityFrameworkCore.SqlServer

    It compiles/builds again :-)

  • User Avatar
    0
    Jurjen created

    Quick question,

    The last hour I've been wondering why the migrations of my module don't get picked up by de DbMigrator. Looking in an older project (v5) I found some code that was not in the EntityFrameworkCore{ApplicationName}DbSchemaMigrator in my V9.0.0 solution:

        /* 
         * Modules Migrations:
         * One call for each AbpModule DbContext.
         */
    
        await _serviceProvider
            .GetRequiredService<{ModuleName}.EntityFrameworkCore.{ModuleName}DbContext>()
            .Database
            .MigrateAsync();
    

    After putting this in it worked like a charm. Is this code supposed to be generated when using 'abp add-module ....' ? or should we just add this manually because I couldn't find this in the documentation. In that case I would have to make a not of that ..

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You shouldn't add EF Core migrations to your module solution.

    The module does not depend on any database provider. It uses EF Core. Your App will decide which database provider to use.

    The module template is designed to be reused in every type of app. EF Core or Mongodb. Monolithic or microservice. So it can't decide what database provider should used.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 26, 2024, 06:07