Skip to content
Last update: May 8, 2024

Extending ApplicationUser

Extending the ApplicationUser entity in the Virto Commerce Platform allows you to add custom fields and behaviors to user profiles. This guide outlines the steps to extend ApplicationUser by creating a new class, ExtendedApplicationUser, and updating the database context accordingly.

To extend an application user:

  1. Create a new class named ExtendedSecurityDbContext derived from SecurityDbContext:

    using Microsoft.EntityFrameworkCore;
    using VirtoCommerce.Platform.Security.Repositories;
    
    public class ExtendedSecurityDbContext : SecurityDbContext
    {
        public ExtendedSecurityDbContext(DbContextOptions<ExtendedSecurityDbContext> options)
            : base(options)
        {
        }
    
        protected ExtendedSecurityDbContext(DbContextOptions options)
            : base(options)
        {
        }
    }
    
  2. Create a temporary migration to initialize the security tables:

    dotnet ef migrations add Temporary
    

    This temporary migration contains the code that creates all the security tables. Delete the temporary files *_Temporary.cs and *_Temporary.Designer.cs, but keep ExtendedSecurityDbContextModelSnapshot.cs.

  3. Define a new class, ExtendedApplicationUser, derived from ApplicationUser, and add any additional fields:

    using VirtoCommerce.Platform.Core.Security;
    
    public class ExtendedApplicationUser : ApplicationUser
    {
        public string NewField { get; set; }
    
        public override void Patch(ApplicationUser target)
        {
            base.Patch(target);
    
            if (target is ExtendedApplicationUser extendedUser)
            {
                extendedUser.NewField = NewField;
            }
        }
    }
    
  4. Override the OnModelCreating() method in ExtendedSecurityDbContext to configure the extended user entity:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Entity<ExtendedApplicationUser>().Property("Discriminator").HasDefaultValue(nameof(ExtendedApplicationUser));
        // Add any additional configuration here
    }
    
  5. Create a new migration to apply the changes made to the database schema:

    dotnet ef migrations add ExtendApplicationUser
    
  6. In the Module.Initialize() method of your module, override ApplicationUser and SecurityDbContext:

    public void Initialize(IServiceCollection serviceCollection)
    {
        // Other initialization code
    
        AbstractTypeFactory<ApplicationUser>.OverrideType<ApplicationUser, ExtendedApplicationUser>();
        serviceCollection.AddTransient<SecurityDbContext, ExtendedSecurityDbContext>();
    }
    

By following these steps, you can seamlessly extend the ApplicationUser entity in the Virto Commerce Platform to accommodate your specific requirements.