abp icon indicating copy to clipboard operation
abp copied to clipboard

Why I can't define custom properties of IdentityUser in Microservice?

Open abllyboy opened this issue 6 years ago • 3 comments

I created a project named 'Demo.EntityFrameworkCore',and then add DemoDbContext.cs,AppUser.cs,DemoDbContextModelCreatingExtensions.cs like the code below: DemoDbContext.cs:

[ConnectionStringName("Default")]
    public class DemoDbContext : AbpDbContext<DemoDbContext>
    {
        public DbSet<AppUser> AppUsers { get; set; }
        public DemoDbContext(DbContextOptions<DemoDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ConfigureDemo();
        }
    }

AppUser.cs:

public class AppUser : FullAuditedAggregateRoot<Guid>, IUser
    {
        #region Base properties

        /* These properties are shared with the IdentityUser entity of the Identity module.
         * Do not change these properties through this class. Instead, use Identity module
         * services (like IdentityUserManager) to change them.
         * So, this properties are designed as read only!
         */

        public virtual Guid? TenantId { get; private set; }

        public virtual string UserName { get; private set; }

        public virtual string Name { get; private set; }

        public virtual string Surname { get; private set; }

        public virtual string Email { get; private set; }

        public virtual bool EmailConfirmed { get; private set; }

        public virtual string PhoneNumber { get; private set; }

        public virtual bool PhoneNumberConfirmed { get; private set; }

        #endregion
        public Guid? OrganizeId { get; set; }
        private AppUser()
        {

        }
    }`

DemoDbContextModelCreatingExtensions.cs :
` public static class DemoDbContextModelCreatingExtensions
    {
        public static void ConfigureDemo(
           this Microsoft.EntityFrameworkCore.ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            builder.Entity<AppUser>(b =>
            {
                b.ToTable("AbpUsers");
                b.ConfigureByConvention();
                b.ConfigureAbpUser();
                b.Property<Guid?>(nameof(AppUser.OrganizeId));
            });
        }
    }

And then reference this project in Microservice's 'AuthorHost' project,and in the 'AuthServerDbContext.cs' I added ConfigureDemo methods,like the code below:

        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.ConfigureIdentity();
            modelBuilder.ConfigureIdentityServer();
            modelBuilder.ConfigureAuditLogging();
            modelBuilder.ConfigurePermissionManagement();
            modelBuilder.ConfigureSettingManagement();
            //this is the code I added
            modelBuilder.ConfigureDemo();
        }

Then set AuthorHost as the default startup project and do the migration,it raise the error "Cannot use table 'AbpUsers' for entity type 'IdentityUser' since it is being used for entity type 'AppUser' and there is no relationship between their primary keys". But when I add the similar code in the project generate by CLI,it works fine.

@maliming

abllyboy avatar Dec 24 '19 04:12 abllyboy

For now you could use the IdentityUser.ExtraProperties? Simply like this:

var user = await UserManager.FindByIdAsync("<UserId here>");
user.ExtraProperties.Add("OrganizeId", "<OrganizeId here>");
await UserManager.UpdateAsync(user);

olicooper avatar Jan 09 '20 22:01 olicooper

@olicooper thank you,I'll try this but this is not strong type.For now I manual add the field in the database and use this code in anther Host ,but this is not the way I want.

abllyboy avatar Jan 11 '20 13:01 abllyboy

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 29 '20 11:07 stale[bot]