Open Closed

Add User as foreign key out of IdentityModule #3806


User avatar
0
mmaldonado@emscltd.com created
  • ABP Framework version: 5.3.4
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): microservices

I need to create the next structure on code

!

The table AbpUsers is on AbpIdentity Database The tables AgencyUser and Agency has to be on Agency Database I need to add the navigation property to the user to get some properties about the user

The documentation related to this case on FAQ are related on the same database, so the map and solutions do not work complete for me.

Current progress

Add reference to package: Volo.Abp.IdentityServer.Domain on Company.Project.AgencyService.EntityFrameworkCore and Company.Project.AgencyService.Domain

Entity Class

public class AgencyUser : FullAuditedTenantEntity<Guid>
{

    // Foreign key referenceing the AbpUser
    public Guid IdentityUserId { get; set; }
    public IdentityUser IdentityUser { get; set; }

    public Guid AgencyId { get; set; }
    public Agency Agency { get; set; }
}
`
> Mapp

`
public class AgencyUserMap : IEntityTypeConfiguration<AgencyUser>
{

    public void Configure(EntityTypeBuilder<AgencyUser> builder)
    {
        builder.ConfigureByConvention();

        builder.ToTable("AgencyUser");

        builder.HasKey(x => x.Id);

        builder.Property(x => x.Id).ValueGeneratedOnAdd();

        builder.HasOne< IdentityUser >().WithMany().HasForeignKey(x => x.IdentityUserId);
        builder.HasOne< Agency >().WithMany().HasForeignKey(x => x.AgencyId);

        builder.Navigation(x => x.IdentityUser).AutoInclude();
        builder.Navigation(x => x.Agency).AutoInclude();
    }
}
  • The problem that I am facing is when I try to generate the migration EF tries to generate IdentityUser tables.
  • I need guidance please your help

7 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can you share the full steps? thanks.

  • User Avatar
    0
    mmaldonado@emscltd.com created

    Hi,

    Can you share the full steps? thanks.

    1. Create the entity Agency (class) (Domain)
    2. Create the entity AgencyUser (Class) (Domain)
    3. Create the map for entities on separate files (EntityFrameworkCore )
    4. Add dbSet for AgencyUser and Agency to dbContext
    5. Call the maps from dbContext
    6. Add reference to package: Volo.Abp.IdentityServer.Domain to EntityFrameworkCore and Domain projects ( I add this package to get the user as FK)

    there are no more steps.

    Could you guide me how should I solve this? or which should be the correct implementation for this ?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Add reference to package: Volo.Abp.IdentityServer.Domain to EntityFrameworkCore and Domain projects

    I remember that ABP app pro startup template already preinstalled those modules, are you using a module template?

  • User Avatar
    0
    mmaldonado@emscltd.com created

    yes, microservice template, it is on first message

  • User Avatar
    -1
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try this:

    • Add Volo.Abp.Identity.Pro.Domain to your Domain project
    • Add Volo.Abp.Identity.Pro.EntityFrameworkCore to your EntityFrameworkCore project
    • Add module dependencies to your module

    Domain

    public class AgencyUser : FullAuditedAggregateRoot<Guid>
    {
        public Guid IdentityUserId { get; set; }
        public IdentityUser IdentityUser { get; set; }
    
        public Guid AgencyId { get; set; }
        public Agency Agency { get; set; }
    }
     
    public class Agency : Entity<Guid>
    {
        public string Name { get; set; }
    }
    

    EntityFrameworkCore

    public class AgencyUserMap : IEntityTypeConfiguration<AgencyUser>
    {
        public void Configure(EntityTypeBuilder<AgencyUser> builder)
        {
            builder.ConfigureByConvention();
    
            builder.ToTable("AgencyUser");
    
            builder.HasKey(x => x.Id);
    
            builder.Property(x => x.Id).ValueGeneratedOnAdd();
    
            builder.HasOne<IdentityUser>().WithMany().HasForeignKey(x => x.IdentityUserId);
            builder.HasOne<Agency>().WithMany().HasForeignKey(x => x.AgencyId);
    
            builder.Navigation(x => x.IdentityUser).AutoInclude();
            builder.Navigation(x => x.Agency).AutoInclude();
            builder.ApplyObjectExtensionMappings();
        }
    }
    
    builder.ApplyConfiguration(new AgencyUserMap());
    
    builder.Entity<Agency>(b =>
    {
        b.ToTable("Agency");
        b.ApplyObjectExtensionMappings();
    });
    
    [ReplaceDbContext(typeof(IIdentityProDbContext))]
    [ConnectionStringName(MyServiceDbProperties.ConnectionStringName)]
    public class MyServiceDbContext : AbpDbContext<MyServiceDbContext> ,  IIdentityProDbContext
    {
        // Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        
        public DbSet<AgencyUser> AgencyUsers { get; set; }
        public DbSet<Agency> Agencies { get; set; }
    
        public MyServiceDbContext(DbContextOptions<MyServiceDbContext> options)
            : base(options)
        {
    
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
    
            builder.ConfigureIdentityPro();
            builder.ConfigureMyService();
        }
    }
    
  • User Avatar
    -1
    liangshiwei created
    Support Team Fullstack Developer

    In this way, your service will use the same database as the identity service.

    In microservices, this is not good, but it's up to you.

    This is another way, you can have redundant users in the service as we did: https://github.com/abpframework/abp/blob/dev/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUser.cs https://github.com/abpframework/abp/blob/dev/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

    The nice thing about it is that you don't have to depend on the identity module

  • User Avatar
    0
    mmaldonado@emscltd.com created

    hello,

    looks like the solution I was looking is https://docs.abp.io/en/commercial/latest/startup-templates/microservice/synchronous-interservice-communication

    I am going to treat the user as an object out of the microservice, I do not want to go over http request because i am inside the same server, but it makes sense, because I could split the microservices into many servers later

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09