Open Closed

Navigation property to another table field in a different module from the current module #5518


User avatar
0
hanntd created

Hi, I have an issue with Foreign Key when we use multiple modules. For example: I have 2 modules:

  • Module 1: HQSOFT.eBiz.GeneralLedger, in this module we have Account entity/table (Id, AcountCode, AccountName...)
  • Module 2: HQSOFT.SharedInformation, in this module we have ReasonCode entity/table (Id, Code, Description, AccountId,....
  • ReasonCode have a FGK is AccountId that reference to Id in Account table. Currently I have 2 issues:
  1. I cannot using Add-migration to create ReasonCode table as it alwasy throw this error: The entity type 'ExtraPropertyDictionary' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943. PM>. I have to create table without FGK then I added manual from DB.
  2. The second issue efcore don't allow to use join in GetQueryForNavigationPropertiesAsync because it couldn't file the Account table in the ShareInformationDbContext and throw the error: Cannot create a DbSet for 'Account' because this type is not included in the model for the context. protected virtual async Task<IQueryable<ReasonCodeWithNavigationProperties>> GetQueryForNavigationPropertiesAsync() { return from reasonCode in (await GetDbSetAsync()) join account in (await GetDbContextAsync()).Set<Account>() on reasonCode.AccountId equals account.Id into accounts from account in accounts.DefaultIfEmpty() select new ReasonCodeWithNavigationProperties { ReasonCode = reasonCode, Account = account }; }:

How can we handle for this case as our solution are separated to many modules and sometime we need to join or get data from other modules. Thanks Dharma (Han Nguyen)


16 Answer(s)
  • User Avatar
    1
    jfistelmann created

    Hey,

    to me it sounds like your answers may be solved by this youtube video from ABP YouTube ABP Framework Entity Framework Core

    There it is described how to merge the dbcontext of different modules into one. Please watch it and tell me if you have any further questions.

  • User Avatar
    0
    hanntd created

    Hi Jfistelmann, Thanks for your information, I can merge two DbContext and do the join query well but having the issue we need to add DbSet for all AggregateRoot/Enties in GL Module although I just need one Aggregate is Account: And the second issue I mentioned in the first post is still cannot resolve, mean I cannot do migration if the table having foreign key refer to the Id in the Account table.

  • User Avatar
    0
    jfistelmann created

    Sorry,

    I've misread your question. Liangshiwei's answer fits better here:

    This is not a good practice. Entities between modules should not have dependencies, modules should be independent.
    Another way is redundant data in the module, like ABP did:、
    •	https://github.com/abpframework/abp/tree/dev/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users
    •	https://github.com/abpframework/abp/blob/dev/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPost.cs#L32
    Cms module has its own user entity, it uses distributed events to synchronize users from the identity module, and then you can use the navigation properties
    
  • User Avatar
    0
    hanntd created

    Thanks for your information but it will be helpful if you have a document guide step by step how I can do in ABP to use distributed events for my case. Thanks, Dharma (Han Nguyen)

  • User Avatar
    0
    jfistelmann created

    Thanks for your information but it will be helpful if you have a document guide step by step how I can do in ABP to use distributed events for my case. Thanks, Dharma (Han Nguyen)

    there you go: https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities#creating-a-new-entity-with-its-own-database-table-collection

  • User Avatar
    0
    hanntd created

    Hi, I'm sorry because I haven't ever used events bus before and I'm quite new to ABP Framework so I still confuse how to retrieve data from an entity in another modules so that I can join with the entity in the current module. Which and where I need to publish and subscribe? Thanks Dharma (Han Nguyen)

  • User Avatar
    0
    jfistelmann created

    Hi, I'm sorry because I haven't ever used events bus before and I'm quite new to ABP Framework so I still confuse how to retrieve data from an entity in another modules so that I can join with the entity in the current module.
    Which and where I need to publish and subscribe?
    Thanks Dharma (Han Nguyen)

    I understand, does this help? https://docs.abp.io/en/abp/latest/Distributed-Event-Bus

  • User Avatar
    0
    hanntd created

    Hi, I still confuse to use Distributed events for my case:

    • I'm in module OM (Order Management) and I want to retrieve list of Customer from Module AR (Account Receivable).
    • And in some cases is vice versa, such as I'm in module AR and I want to retrieve list of Orders (in OM module) of a customer in AR module. Could you please give me a sample code or workflow for my case? Thanks
  • User Avatar
    0
    hanntd created

    Hi @jfistelmann, I tried to use Dynamic API Client Proxy as the guide from here: https://docs.abp.io/en/abp/latest/API/Dynamic-CSharp-API-Clients

    1. In the GL module I have Account (finance account) Entity, this entity will be accessed by other modules. I build each projects in this module into nuget packages and push to package management server.
    2. In the appsetting of HttpApi.Host of this module I changed Auth server to the AuthServer of OM Module. Then I started only HttpApi.Host of GL Module.
    3. In Blazor project of another module I add reference to GLApplication.Contract package and in the blazor UI application I inject AccountsAppService to call GetListAsync. Then I start Auth, Blazor.Host, HttpApi.Host of this module.
    4. When run the blazor screen having call to AccountsAppService in GL module it throw the error:
    5. In the razor page I have added this code:
    6. If I review the Authorize code above it throw another error:

    We are stuck on this problem two weeks, please advise us which approach will be best for our case when we need to exchange data between modules in our application. Thanks

  • User Avatar
    0
    jfistelmann created

    hey hanntd,

    that's not how things work. can you provide some information on your setup?

    You said you're using blazor. I need to know:

    • ef or mongo db
    • dbms (postgres, ms sql etc)
    • tiered or not or microservice

    so that i can use abp cli to create a repro.

    Then I can create some resources on how to tackle the issues you are facing.

  • User Avatar
    0
    hanntd created

    Hi Jfistelmann, I'm using Postgres and not using micro service. We are separating function by module such as General Ledger, Inventory, Order Management, Account Payable, Account Receivable... these module should be accessible together. We develop these module dependently then we build into nutget package and integrate to Main solution. For integration to Main solution we have no issue, but now we have problem with modules during development as I raised above. So the main problem here how we can access entity/data between modules (two-ways). For example, from module Order Management (OM) I can getlist of customer from Account Receivable (AR) module and from AR I can access to OM to getlist orders... Thanks, Dharma (Han Nguyen)

  • User Avatar
    0
    hanntd created

    I already resolved the Authorization error, I need to add GeneralLedger to resource in OpenIddictScopes table now it can be access through Dynamic API Client Proxy. But I still need your advice on the model, do we need to use Distributed Events for this case which one is better, using Dynamic API or Distributed Events?

  • User Avatar
    0
    jfistelmann created

    Hey,

    I'll create some resources. Wanted to make a blog post on how to handle such cases along with source code.

    Takes a bit of time, but I think that it would bring the best value.

  • User Avatar
    0
    hanntd created

    That's great, thank you so much and waiting for you blog post.

  • User Avatar
    0
    jfistelmann created

    That's great, thank you so much and waiting for you blog post.

    Here it is: https://chrobyte.de/blogs/default/abp-module-development-and-cross-module-dependencies

    Also submitted to abp community, but may take a while until approved and available.

  • User Avatar
    0
    hanntd created

    Thank you so much for your helpful post.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19