- 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)
-
0
Hi,
Can you share the full steps? thanks.
-
0
Hi,
Can you share the full steps? thanks.
- Create the entity Agency (class) (Domain)
- Create the entity AgencyUser (Class) (Domain)
- Create the map for entities on separate files (EntityFrameworkCore )
- Add dbSet for AgencyUser and Agency to dbContext
- Call the maps from dbContext
- 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 ?
-
0
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?
-
0
yes, microservice template, it is on first message
-
-1
Hi,
You can try this:
- Add
Volo.Abp.Identity.Pro.Domain
to yourDomain
project - Add
Volo.Abp.Identity.Pro.EntityFrameworkCore
to yourEntityFrameworkCore
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(); } }
- Add
-
-1
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
-
0
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