Open Closed

How to Add the User Entity and Role Entity as a Navigation Property of Another Entity #865


User avatar
0
gvnuysal created
  • ABP Framework version: v3.3.1
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Seperated (Angular): no

Hi support, We have two question,

  1. There is an entity associated with AbpUser.(We tried the this blog post. https://community.abp.io/articles/abp-suite-how-to-add-the-user-entity-as-a-navigation-property-of-another-entity-furp75ex) The navigation properties of the entities created with the abp suite do not have foreign keys. We have to do this manually.

    After Migration,We have an error,

    Links to the solution methods we found. https://github.com/abpframework/abp/issues/7105#issuecomment-757641838 https://github.com/abpframework/abp/issues/1414 But,Our problem continues. We could not solve it.

  2.  How to Add the Role Entity as a Navigation Property of Another Entity?
    

    Not: our license type is team.


2 Answer(s)
  • User Avatar
    0
    Moyaoxiang created

    Hi gvnuysal, This is not a good practice, you can refer to this Issue to operate, it can work normally in my project. DeviceEntity

    public class Device : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        public virtual Guid? TenantId { get; set; }
    
        [NotNull] public virtual string Name { get; set; }
    
        public virtual AppUser User { get; set; }
    
        public virtual Guid AppUserId { get; set; }
    
        public virtual IdentityRole Role { get; set; }
    
        public virtual Guid RoleId { get; set; }
    
        public Device()
        {
        }
    
        public Device(Guid id, string name)
        {
            Id = id;
            Check.NotNull(name, nameof(name));
            Check.Length(name, nameof(name), DeviceConsts.NameMaxLength, 0);
            Name = name;
        }
    }
    

    MigrationsDbContext

    public class IoTProjectMigrationsDbContext : AbpDbContext<IoTProjectMigrationsDbContext>
    {
        public IoTProjectMigrationsDbContext(DbContextOptions<IoTProjectMigrationsDbContext> options)
            : base(options)
        {
    
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
    
            /* Include modules to your migration db context */
    
            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureLanguageManagement();
            builder.ConfigureSaas();
            builder.ConfigureTextTemplateManagement();
            builder.ConfigureBlobStoring();
    
            /* Configure your own tables/entities inside the ConfigureIoTProject method */
    
            builder.Entity<AppUser>(b =>
            {
                b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
    
                b.ConfigureByConvention();
                b.ConfigureAbpUser();
                b.HasOne<IdentityUser>().WithOne().HasForeignKey<AppUser>(e => e.Id);
    
                /* Configure mappings for your additional properties.
                    * Also see the IoTProjectEfCoreEntityExtensionMappings class.
                    */
            });
            
            builder.ConfigureIoTProject();
        }
    }
    

    DbContextModelCreatingExtensions

    public static class IoTProjectDbContextModelCreatingExtensions
    {
        public static void ConfigureIoTProject(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));
    
            /* Configure your own tables/entities inside here */
    
            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(IoTProjectConsts.DbTablePrefix + "YourEntities", IoTProjectConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});
    
            builder.Entity<Device>(b =>
            {
                b.ToTable(IoTProjectConsts.DbTablePrefix + "Devices", IoTProjectConsts.DbSchema);
                b.ConfigureByConvention();
                b.Property(x => x.TenantId).HasColumnName(nameof(Device.TenantId));
                b.Property(x => x.Name).HasColumnName(nameof(Device.Name)).IsRequired().HasMaxLength(DeviceConsts.NameMaxLength);
                
                b.HasOne(x=>x.User).WithMany().HasForeignKey(x => x.AppUserId);
                b.HasOne(x=>x.Role).WithMany().HasForeignKey(x => x.RoleId);
            });
        }
    }
    

    DbContext

    [ConnectionStringName("Default")]
    public class IoTProjectDbContext : AbpDbContext<IoTProjectDbContext>
    {
        public DbSet<Device> Devices { get; set; }
        public DbSet<AppUser> Users { get; set; }
    
        /* Add DbSet properties for your Aggregate Roots / Entities here.
            * Also map them inside IoTProjectDbContextModelCreatingExtensions.ConfigureIoTProject
            */
    
        public IoTProjectDbContext(DbContextOptions<IoTProjectDbContext> options)
            : base(options)
        {
    
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
    
            /* Configure the shared tables (with included modules) here */
    
            builder.Entity<AppUser>(b =>
            {
                b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
    
                b.ConfigureByConvention();
                b.ConfigureAbpUser();
    
                /* Configure mappings for your additional properties.
                    * Also see the IoTProjectEfCoreEntityExtensionMappings class.
                    */
            });
    
            /* Configure your own tables/entities inside the ConfigureIoTProject method */
    
            var options = new IdentityModelBuilderConfigurationOptions(
                AbpIdentityDbProperties.DbTablePrefix,
                AbpIdentityDbProperties.DbSchema
            );
            
            builder.Entity<IdentityRole>(b =>
            {
                b.ToTable(options.TablePrefix + "Roles", options.Schema);
    
                b.ConfigureByConvention();
    
                b.Property(r => r.Name).IsRequired().HasMaxLength(IdentityRoleConsts.MaxNameLength);
                b.Property(r => r.NormalizedName).IsRequired().HasMaxLength(IdentityRoleConsts.MaxNormalizedNameLength);
                b.Property(r => r.IsDefault).HasColumnName(nameof(IdentityRole.IsDefault));
                b.Property(r => r.IsStatic).HasColumnName(nameof(IdentityRole.IsStatic));
                b.Property(r => r.IsPublic).HasColumnName(nameof(IdentityRole.IsPublic));
    
                b.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
    
                b.HasIndex(r => r.NormalizedName);
            });
            
            builder.ConfigureIoTProject();
        }
    }
    

    Repository

    public class EfCoreDeviceRepository : EfCoreRepository<IoTProjectDbContext, Device, Guid>, IDeviceRepository
    {
        public EfCoreDeviceRepository(IDbContextProvider<IoTProjectDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }
    
        public override async Task<IQueryable<Device>> WithDetailsAsync()
        {
            return (await base.WithDetailsAsync()).Include(t => t.Role).Include(t => t.User);
        }
    
        // ...
    }
    

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11