Open Closed

Module connection string with different datababse and Migration #8028


User avatar
0
imranStem created

I have created new module and add each project references to main application project like application into application, ef into ef module. When I add migration with module dbContext then It throws error that no dbContext was found. The startup project is main host application and selected project is main entity framework module.

  • ABP Framework version: v8.3.0
  • UI Type: Angular
  • Database System: EF Core
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

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

    hi

    When I add migration with module dbContext then It throws error that no dbContext was found. The startup project is main host application and selected project is main entity framework module.

    Please share the command, execution folder and exception details.

    Can you try to run dotnet ef migrations add TestName in your application ef core project?

    Thanks

  • User Avatar
    0
    imranStem created

    Execution command : dotnet ef migrations add TestName Folder: Ef core module in main application

    Migration run successfully but only with abp entities, not module entity. I have add reference of module ef core in main ef core module and also add DependsOn ef core module ref to main ef core module.

    Do I need to do any other configuration to add module entities migration?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You need to call builder.ConfigureYourModule(); in the OnModelCreating method of your application DbContext class.

  • User Avatar
    0
    imranStem created

    I have already called builder.ConfigureYourModule(); in module class, If I add builder.ConfigureYourModule(); in main application ef core module then it will create entities in same database, not in module database. Right?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    There is no module database. Only apps have a database.

    https://github.com/abpframework/abp/blob/dev/modules/openiddict/app/OpenIddict.Demo.Server/EntityFrameworkCore/ServerDbContext.cs#L32

    https://github.com/abpframework/abp/blob/dev/modules/openiddict/src/Volo.Abp.OpenIddict.EntityFrameworkCore/Volo/Abp/OpenIddict/EntityFrameworkCore/OpenIddictDbContextModelCreatingExtensions.cs#L12

  • User Avatar
    0
    imranStem created

    I mean one apps can have multiple databases connection string, Like If I want abp audit log in different database then we can configure that same way, I want different database for custom module. One is default connection string and ModuleConnectionString.

    public static class ModuleDbProperties
    {
        public static string DbTablePrefix { get; set; } = "Module";
    
        public static string? DbSchema { get; set; } = null;
    
        public const string ConnectionStringName = "Module";
    }
    
  • User Avatar
    0
    imranStem created

    I have add builder.ConfigureYourModule() and migration is updating default database not module database.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I want different database for custom module. One is default connection string and ModuleConnectionString.

    See https://abp.io/docs/latest/framework/fundamentals/connection-strings https://abp.io/docs/latest/framework/data/entity-framework-core/migrations

  • User Avatar
    0
    imranStem created

    However, this can work only if the audit log database with the given connection string is available. So, you need to create the second database, create audit log tables inside it and maintain the database tables. No problem if you manually do all these. However, the recommended approach is the code first migrations. One of the main purposes of this document is to guide you on such database separation scenarios.

    Ok, I understand, The migration for module can be manage by module itself, not the application.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    : )

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35