Open Closed

No db migrations for Entities in Modules #9011


User avatar
0
viktor created

We are using ABP Studio + ABP Suite to generate Entities. We created Main and one [Module1]. We di install Module into Main.
After, using Abp Suite we select [Module1] and created new entity. But no Migration was generated to apply on database level.

How to generate db migrations for new entities of [Module1]. We plan to add/use more Modules and make complex entities including Navigations and Master-child. So ABP Suite was very helpful tool.


11 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    While you developing a Module, you shouldn't create migrations inside module since it's re-usable project that can be consumed by multiple applications. You should create migrations in your end application that consumes your module.

    You can call builder.ConfigureModule1(); extension method in your application's DbContext to configure entities from your module and then create migrations in application. If you created DDD Module while creating it, there should already be Module1DbContextModelCreatingExtensions.cs and you can configure your entities there, you can configure entities from module in that extension method nad use it wherever you use use that module like any other ABP modules.

  • User Avatar
    0
    viktor created

    Hi, enisn
    I am not creating migrations. We are using ABP Studio + ABP Suite. I hope it will help to understand our goal: we want to use ABP Suite for creating Entities to be sure all needed code will be generated properly.

    Below steps to reproduce issues:

    1. ABP Studio - New Solution [DemoSolution]: Angular LeptonX + EF + SQLite (in prod scenario we will use SQL) + Multi-Tenancy

    2. Solution Created. Build. Run. Works.

    3. ABP Studio - New Module [FirstModule] - DDD Module.

    4. ABP Studio - [DemoSolution] -> Import Module + Install. Graph Build. Run. Ok.
      And now we want to add new Entites to [FirstModule] module. So:

    5. ABP Suite - version 9.1.0. Selecting FirstModule
      image.png

    6. Add new Master Entity - [Product] with one property [Title]. Save and Generate. Ok, no errors.
      image.png
      Also I can see [FirstModule] under permissions, and I did grant permissions to admin role. Ok, no errors.
      image.png

    Issue 1 >> I can not find any tables in database. There are no any migrations files create so I can not run dotnet ef migrations list or update database.
    image.png

    Issue 2 >> I can not see [FirstModule] itself on Angular UI. How to manage (CRUD) Product?
    image.png

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, enisn
    I am not creating migrations. We are using ABP Studio + ABP Suite. I hope it will help to understand our goal: we want to use ABP Suite for creating Entities to be sure all needed code will be generated properly.

    Below steps to reproduce issues:

    1. ABP Studio - New Solution [DemoSolution]: Angular LeptonX + EF + SQLite (in prod scenario we will use SQL) + Multi-Tenancy

    2. Solution Created. Build. Run. Works.

    3. ABP Studio - New Module [FirstModule] - DDD Module.

    4. ABP Studio - [DemoSolution] -> Import Module + Install. Graph Build. Run. Ok.
      And now we want to add new Entites to [FirstModule] module. So:

    5. ABP Suite - version 9.1.0. Selecting FirstModule
      image.png

    6. Add new Master Entity - [Product] with one property [Title]. Save and Generate. Ok, no errors.
      image.png
      Also I can see [FirstModule] under permissions, and I did grant permissions to admin role. Ok, no errors.
      image.png

    Issue 1 >> I can not find any tables in database. There are no any migrations files create so I can not run dotnet ef migrations list or update database.
    image.png

    Issue 2 >> I can not see [FirstModule] itself on Angular UI. How to manage (CRUD) Product?
    image.png

    Hi, did you check our Modular Monolith Development Tutorial?. In that tutorial, we are explaining important points that you may want to check.

    Currently, we also preparing its Angular UI version. To see the related menu items for now, you should write some codes, as mentioned https://github.com/abpframework/abp/issues/20827#issuecomment-2624232973

    Please check the tutorial and the issue comment to see if it fixes your problem. Regards.

  • User Avatar
    0
    viktor created

    Hi, EngincanV
    Yes, we read this tutorial. But how it is related? You did not answer about migration files? how can we generate migrations for new Entities which are created in Modules? Is it some bug or it should be done always manually? If it is manual process, so what is then reason to pay for Suite... a lot of logic will be in Modules.

    And, yes we did changes mentioned in #20827, with some fixes in generated code it's working now.

    Awaiting answer on first issue.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, first of all, thanks for the quick reply. Great to hear that, the code is working now. For the first question:

    You did not answer about migration files? how can we generate migrations for new Entities which are created in Modules? Is it some bug or it should be done always manually? If it is manual process, so what is then reason to pay for Suite... a lot of logic will be in Modules.

    ABP Suite creates migrations and applies them to most of the templates. However, for the module templates, since ABP Suite can't know, which module has relation to which main solution, it's hard to get the migration folder and apply it. So, only for the module template, you should create migrations manually, yes.

  • User Avatar
    0
    viktor created

    Hi, EngincanV
    But I am talking about DB migrations. Suite knows Entity and all relations within one module (and there are no cross-module entities relations). So why it can not generate db migrations? Is it plans for such in future?
    We are building solution to separate some business logic in different modules, so a lot of new Entities are planned inside Modules, and to build always manually it would be a lot of operational risks.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, EngincanV
    But I am talking about DB migrations. Suite knows Entity and all relations within one module (and there are no cross-module entities relations). So why it can not generate db migrations? Is it plans for such in future?
    We are building solution to separate some business logic in different modules, so a lot of new Entities are planned inside Modules, and to build always manually it would be a lot of operational risks.

    Hi @Viktor, I understand your concerns. There was not any open issue about that but I've created just before (with issue number #19581 / in our internal repository). So, we will try to prioritize it and support in the future.

    Thanks for your understanding, regards.

  • User Avatar
    0
    viktor created

    Hi, EngincanV
    So I need some example how to build db migration file for entities inside Modules.
    May you show some example code here. For Example I want to add new entity [Product] with properties [Title], [Price]. So I migration files will be look like?

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, EngincanV
    So I need some example how to build db migration file for entities inside Modules.
    May you show some example code here. For Example I want to add new entity [Product] with properties [Title], [Price]. So I migration files will be look like?

    Hi, in the modular monolith approach, we suggest to create migrations in the host application's ef core project. But, if you still want to create a migration in the modules, then you can create a DbContextFactory class that implements IDesignTimeDbContextFactory<OrderingServiceDbContext> (like in the main application, you can check its implementation) and then basically execute the dotnet ef migrations add MigrationName in your module to create migrations.

  • User Avatar
    0
    viktor created

    Hi, EngincanV
    I can not understand your answers... I will try ask different way.
    I am using only ABP Suite to make all Entities. I never write any code as all parts are generated automatically, and I can create all needed Entities with properties using ABP Suite only. I am not dealing with Visual Studio as it was not part of my duties :)

    As I mentioned I did Step 6 in ABP Suite. I created new Entity with properties. And then I want to run dotnet ef database update to have tables in database, but I have no migration file generated. So I can not create tables. So please provide steps I must do after my Step 6.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, EngincanV
    I can not understand your answers... I will try ask different way.
    I am using only ABP Suite to make all Entities. I never write any code as all parts are generated automatically, and I can create all needed Entities with properties using ABP Suite only. I am not dealing with Visual Studio as it was not part of my duties :)

    As I mentioned I did Step 6 in ABP Suite. I created new Entity with properties. And then I want to run dotnet ef database update to have tables in database, but I have no migration file generated. So I can not create tables. So please provide steps I must do after my Step 6.

    Hi, sorry for the inconvenience. I will explain in detail:

    • After you imported the [FirstModule] into the main application [DemoSolution] and generated the CRUD pages for [FirstModule], as you said it does not create a new migration.

    • At that point, you can build the [DemoSolution] (/graphBuild) and then create a new migration via ABP Studio or dotnet-ef global tool like in the following figure:

    image.png

    Right click to the [DemoSln.EntityFrameworkCore] project, then select Ef Core CLI -> Add migration. It will open a dialog as below:

    image.png

    Write a migration name and then click the OK button to create a migration.

    image.png

    • After creating the migration, you can check from the Migrations folder under your [DemoSln.EntityFrameworkCore] project and see the generated migration:

    image.png

    • Then, you can update your database by either using the dotnet ef database update command in the [DemoSln.EntityFrameworkCore] project or via ABP Studio:

    image.png

    image.png

    • Then, in your database, you should see the table:

    image.png

    image.png


    I hope I was able to explain it in detail. If there is a step that is unclear, feel free to ask.
    Regards.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.2.0-preview. Updated on March 25, 2025, 11:10