Open Closed

How to create one to many relationship? #9259


0
marble68 created

8.9, jquery.

Scenario: An employee can belong to multiple departments. Using the Power Tool, I can pick a FK relationship, but it's only one to one.

Per this post: https://support.aspnetzero.com/QA/Questions/6933/How-to-generate-relational-entities-using-abp-Power-tool One to Many is supported.

How do we do this?

If this is no longer the case? How do we accomplish this in the asp.net zero / aspboilerplate way?


4 Answer(s)
  • 0
    maliming created
    Support Team

    hi

    You can check the documentation.

    https://docs.aspnetzero.com/en/common/latest/Development-Guide-Rad-Tool#navigation-properties

    https://docs.microsoft.com/en-us/ef/core/modeling/relationships

  • 0
    marble68 created

    Ok - the Rad tool only does one-to-one.

    Examining the MS documents - I see I can add, for example List<Department> Departments to my Employee entity.

    However, I think this will break migrations.

    Thus, I'm looking at the User and Roles - it seems the netZero / ABP way is to:

    • Have a role table
    • Have a UserRoles Table
    • Have a Users table

    On the GetUserForEditOutput class has an Array of UserRoleDto.

    When GetUserForEdit is call - it goes to the RoleManager which gets all roles, then it selects those into a UserRoleDto.

    Then it adds that to the GetUserForEditOutput class and returns it.

    On Update or Save - it uses CreateOrUpdateUserInput, which has role names as an array of Strings[].

    On update, it sets the roles via the UserManager, which leans on the RoleManager - and it does all this by the role name.

    Internally, this takes this array string, finds each role by normalized name, finds the role, then deletes it from UserRoleDto by the Role.Id, or adds it to the UserRoleDto table.

    In the modal create or edit js - on save, it goes through the list of checked roles, and builds a string by name, assigns the model, and that's posted back.

    This list are the checkboxes under the wrapper div of class user-role-checkbox-list.

    The view model is CreateOrEditUserModalViewModel, which is inheriting from GetUserForEditOutput, which has the UserRoleDto array (as mentioned above.

    The User inherits from AbpUser which is based on AbpUserBase, which has ICollection<UserRole> Roles.

    For mapping, in the customDtoMapper - there's configuration.CreateMap<Role, OrganizationUnitRoleListDto>(); Under the User object, which seems to have more to do the OUs than the actual Roles' ICollection<UserRole>.

    The object mapper does not handle roles, instead it leans on the UserManager. It goes through each role, and calls IsInRoleAsync from the userManager, which then goes to our UserRole table and queries it, returning a bool if the list of roles the user has contains the passed name.

    It does this each time (inefficient, IMHO, but irrelevant).

    So, based on this - to accomplish what I would need to do the following:

    I create an Entity for Employee Department membership I modify the DTOs for my employee to have an Arrays / Lists for the EmployeeDepartment entity. Then, in my Employee App Service, I populate the EmployeeDepartment list / array on the Employee DTOs. In the view model, I add this array of EmployeeEntityDTOs, rendering it however I wish (perhaps a modal). Then, on post back, when saving the employee, I also have to update my EmployeeDepartment entity, adding new entries and removing the old ones.

    So, in summary, my requirements are:

    • A relationship entity
    • Modify DTOs
    • Modify View Models
    • Modify client side scripts to populate Models
    • Modify App service to handle the Lists/Arrays of the primary Entity.

    Am I missing anything?

  • 0
    ismcagdas created
    Support Team

    Hi @marble68,

    Yes, AspNet Zero defines some relationships in that way, but you can do it as you wish. There are no restrictions on AspNet Zero side. Did you manage to make it work ?

  • 0
    marble68 created

    Yes. But I'm still working it out.