- 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:
- Is manually setting up the navigation properties the right approach for establishing these many-to-many relationships?
- 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)
- 
    0hi IdentityUser class is not in your application, it comes from the Identity module as you know. So, you can not directly change its source code to add a navigation collection (unless you include Identity modules's source code into your solution). See https://abp.io/support/questions/2954/Unable-To-Define-Many-To-Many-IdentityUser-Relationship#answer-315c2908-13b3-3dd2-b0cc-3a03ba043eb3 
 
                                