Open Closed

Many to Many rel with Identity User and modify/extend the User edit page #9064


User avatar
0
InnMarco created
  • Template: app-nolayers

  • Created ABP Studio Version: 0.9.25

  • Current ABP Studio Version: 0.9.25

  • Multi-Tenancy: No

  • UI Framework: blazor-server

  • Theme: leptonx

  • Theme Style: system

  • Run Install Libs: Yes

  • Database Provider: ef

  • Database Management System: postgresql

  • Create Initial Migration: Yes

  • Run Db Migrator: Yes

  • Use Local References: No

  • Optional Modules:

    • GDPR

    • FileManagement

    • TextTemplateManagement

    • LanguageManagement

    • AuditLogging

    • Chat

    • OpenIddictAdmin

Hello, I need advice on the best way to implement many-to-many relationships involving the User entity. Here’s my scenario:

Entities Involved:
*Work Sites and Services: I have several dynamic work sites and services.
*User Types: There are two types of users:
* Internal users: They can add and modify services and sites.
* Commercial (customer) users: They are associated with specific services and sites and should only be able to view tasks and activities assigned to those sites.
Relationships:

  • I want to establish a many-to-many relationship between IdentityUser and Service.

  • I also want a many-to-many relationship between IdentityUser and Site.
    Current Approach:

  • I noticed that ABP Suite doesn’t support creating many-to-many relationships with an ABP Entity directly.

  • Therefore, I manually configured the navigation properties between Service and IdentityUser.

Questions:

  1. Is manually setting up the navigation properties the right approach for establishing these many-to-many relationships?

  2. How can I modify the User edit page to include these two relationships? My goal is to assign services and sites to a user, which will then be used to filter data.
    Could you please advise on the best practices and steps to achieve this?

Thanks

I share code about Services and many to many rel ServiceIdentityUser and his joint table

namespace CoopService.Services
{
    public abstract class ServiceBase : FullAuditedAggregateRoot<int>
    {
        [NotNull]
        public virtual string Code { get; set; }

        [NotNull]
        public virtual string Description { get; set; }

        public ICollection<ServiceIdentityUser> IdentityUsers { get; private set; }

        protected ServiceBase()
        {

        }

        public ServiceBase(string code, string description)
        {

            Check.NotNull(code, nameof(code));
            Check.Length(code, nameof(code), ServiceConsts.CodeMaxLength, 0);
            Check.NotNull(description, nameof(description));
            Code = code;
            Description = description;
            IdentityUsers = new Collection<ServiceIdentityUser>();
        }
        public virtual void AddIdentityUser(Guid identityUserId)
        {
            Check.NotNull(identityUserId, nameof(identityUserId));

            if (IsInIdentityUsers(identityUserId))
            {
                return;
            }

            IdentityUsers.Add(new ServiceIdentityUser(Id, identityUserId));
        }

        public virtual void RemoveIdentityUser(Guid identityUserId)
        {
            Check.NotNull(identityUserId, nameof(identityUserId));

            if (!IsInIdentityUsers(identityUserId))
            {
                return;
            }

            IdentityUsers.RemoveAll(x => x.IdentityUserId == identityUserId);
        }

        public virtual void RemoveAllIdentityUsersExceptGivenIds(List<Guid> identityUserIds)
        {
            Check.NotNullOrEmpty(identityUserIds, nameof(identityUserIds));

            IdentityUsers.RemoveAll(x => !identityUserIds.Contains(x.IdentityUserId));
        }

        public virtual void RemoveAllIdentityUsers()
        {
            IdentityUsers.RemoveAll(x => x.ServiceId == Id);
        }

        private bool IsInIdentityUsers(Guid identityUserId)
        {
            return IdentityUsers.Any(x => x.IdentityUserId == identityUserId);
        }
    }
}
namespace CoopService.Services
{
    public class ServiceIdentityUser : Entity
    {

        public int ServiceId { get; protected set; }

        public Guid IdentityUserId { get; protected set; }

        private ServiceIdentityUser()
        {

        }

        public ServiceIdentityUser(int serviceId, Guid identityUserId)
        {
            ServiceId = serviceId;
            IdentityUserId = identityUserId;
        }

        public override object[] GetKeys()
        {
            return new object[]
                {
                    ServiceId,
                    IdentityUserId
                };
        }
    }
}

1 Answer(s)
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