Base solution for your next web application
Open Closed

Can I create a *.EntityFramework project for each module #1970


User avatar
0
trendline created

As we know, if we create a new solution then come with below projects(asp.net core version) MyCompany.ApbZeroTemplate.Application MyCompany.ApbZeroTemplate.Core MyCompany.ApbZeroTemplate.EntityFramework MyCompany.ApbZeroTemplate.Migrator MyCompany.ApbZeroTemplate.Web

If I add a new module A with modular structure like below MyCompany.ApbZeroTemplate.Application MyCompany.ApbZeroTemplate.Core MyCompany.ModuleA.Application MyCompany.ModuleA.Core MyCompany.ApbZeroTemplate.EntityFramework MyCompany.ApbZeroTemplate.Migrator MyCompany.ApbZeroTemplate.Web

Is there an approach I can add a separated ModuleA.EntityFramework project to manage DbSets for ModuleA? If so, how to do database migration? If not, is there an approach that I can manage the DbSets of ModuleA in a separated place? Not all under MyCompany.ApbZeroTemplate.EntityFramework?


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Sure, you can do it. Managing migrations is hard because EF does not support well for such modularity especially if some of your database tables intersects (say, your module dbcontextes inherits from a base dbcontext which contains your shared entities). You have different options, each have it's own difficulties. If this module will only be used in your application(s), I would suggest you to create a single dbcontext, then in this dbcontext's OnModelCreating, call each module's some 'configure' static method to add configuration/entities of this module to the dbcontext. In this approach, you can even re-use same module in different application easily. I suggest you to search web for alternatives. But dbcontext inherits have always migration problems which you should handle manually.

  • User Avatar
    0
    trendline created

    Could you write some walkthrough code?

    If more than one DbContext under the *.EntityFramework.project, and other DbContexts also inherit from AbpZeroDbContext, that will cause the DbSets in AbpZeroDbContext be instanced more than once?

  • User Avatar
    0
    hikalkan created
    Support Team

    If you have 2 non-abstract dbcontext, then you will have 2 different migrations normally. If both they inherits from AbpZeroDbContext, then your base tables will be duplicated between 2 migrations. So, you should manually remove them from one of the migrations. I suggest you to use single dbcontext if you have single database. It will be easier. One ABP developer tried a similar thing. You can see this issue: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1576">https://github.com/aspnetboilerplate/as ... ssues/1576</a> There is a code sample there. it will not directly work in your case but can provide an idea for you.