Open Closed

How can I configure a Core EF5 M2M using IdentityUser #1962


User avatar
0
okains created
  • ABP Framework version: v4.4.3
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

I would like to know what is the best practice using ABP for creating a Many to Many relationship with IdentityUser? With Core EF5 we can add nav properties to our entities to make this easy, as documented here : https://www.davidhayden.me/blog/ef-core-5-many-to-many-relationships.

I created 2 test case entities, M2MUser & M2MGroup, added the navigation collections i.e. ' public ICollection<M2MUser> M2MUsers { get; set; } ' in the domain project and ran a DB Migration and everything worked as it should do, the M2MUserM2MGroup table was created in the DB.

However we can not add a collection property to the IdentityUser class, and there is no longer a derived ABPUser class. If we want to add an M2M relationship with a 'User' should we wrap IdentityUser? This seems like the wrong way forward, am I missing something? Can you please provide the best practice, using ABP, for this situation?

If this helps, here is the part of my model that I am trying to implement. 'User' is an IdentityUser. 'OnboardingGoal' is an entity I have created using ABPSuite. UserOnboardingGoal should be implicitly generated by EF, so it is not explicitly created using ABPSuite.

Thanks,

Karim

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

1 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    You directly add many-to-many navigation properties for your own entitys, but not IdentityUser, because the IdentityUser is a pre-built entity.

    The best way to pre-build entities to add a many-to-many relationship is to use an middle table.

    Example:

    public class MyGroup : Entity<Guid>
    {
        public string Name { get; set; }
    
        public List<UserGroup> Users { get; set; }
    }
    
    public class UserGroup : Entity
    {
        public Guid GroupId { get; set; }
    
        public MyGroup Group { get; set; }
    
        public Guid UserId { get; set; }
    
        public IdentityUser User { get; set; }
        public override object[] GetKeys()
        {
            return new object[] { UserId, GroupId };
        }
    }
    
    builder.Entity<MyGroup>(b =>
    {
        b.ConfigureByConvention();
    });
    
    builder.Entity<UserGroup>(b =>
    {
        b.HasKey(x => new { x.UserId, x.GroupId });
        b.HasOne(x => x.Group).WithMany(x => x.Users).HasForeignKey(x => x.GroupId);
        b.HasOne(x => x.User).WithMany().HasForeignKey(x => x.UserId);
    });
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 21, 2026, 06:18
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.