Extend 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:
-
Create a new class named
ExtendedSecurityDbContext
derived fromSecurityDbContext
: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) { } }
-
Create a temporary migration to initialize the security tables:
This temporary migration contains the code that creates all the security tables. Delete the temporary files
*_Temporary.cs
and*_Temporary.Designer.cs
, but keepExtendedSecurityDbContextModelSnapshot.cs
. -
Define a new class,
ExtendedApplicationUser
, derived fromApplicationUser
, 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; } } }
-
Override the
OnModelCreating()
method inExtendedSecurityDbContext
to configure the extended user entity: -
Create a new migration to apply the changes made to the database schema:
-
In the
Module.Initialize()
method of your module, overrideApplicationUser
andSecurityDbContext
:
By following these steps, you can seamlessly extend the ApplicationUser
entity in the Virto Commerce Platform to accommodate your specific requirements.