Abp: Add AppUser to the MVC startup template

Created on 15 Feb 2019  路  2Comments  路  Source: abpframework/abp

Hi.
According to your recommendations, we should not use IdentityUser/IdentityUserRole in our application. Please add basic AppUser infrastructures to application templates.
Thanks in advance.

abp-template-app feature

Most helpful comment

What's done?

  • I added AppUser to the domain project.
  • I configured to share the same AbpUsers table between the application and the Identity module.
  • I also created a new separated DbContext, MyProjectNameMigrationsDbContext, to manage db migrations.

How To Use?

  • Just inject IRepository<AppUser, Guid> as use as normally you do.
  • You can define your custom properties into this class (see the example below).
  • You never create or delete this entity, because it is Identity module's job.
  • You can query users from database with this entity.
  • You can update values of your custom properties. You can not update Identity's properties, they are readonly. See the AppUser class, I have written comments.

Example: Adding a new property to the AppUser

  1. Define the property in the AppUser class:

csharp public virtual int Reputation { get; set; }

  1. Map the property inside the MyProjectNameDbContextModelCreatingExtensions.ConfigureCustomUserProperties method:

csharp b.Property<int>(nameof(AppUser.Reputation)).IsRequired().HasDefaultValue(1);

  1. Add new migration using Package Manager Console:

Add-Migration "Added_AppUser_Reputation"

This will produce a migration like this:

````csharp
public partial class Added_AppUser_Reputation : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
name: "Reputation",
table: "AbpUsers",
nullable: false,
defaultValue: 1);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "Reputation",
        table: "AbpUsers");
}

}
````

  1. Update the database schema:

Update-Database

All 2 comments

That can be good.

What's done?

  • I added AppUser to the domain project.
  • I configured to share the same AbpUsers table between the application and the Identity module.
  • I also created a new separated DbContext, MyProjectNameMigrationsDbContext, to manage db migrations.

How To Use?

  • Just inject IRepository<AppUser, Guid> as use as normally you do.
  • You can define your custom properties into this class (see the example below).
  • You never create or delete this entity, because it is Identity module's job.
  • You can query users from database with this entity.
  • You can update values of your custom properties. You can not update Identity's properties, they are readonly. See the AppUser class, I have written comments.

Example: Adding a new property to the AppUser

  1. Define the property in the AppUser class:

csharp public virtual int Reputation { get; set; }

  1. Map the property inside the MyProjectNameDbContextModelCreatingExtensions.ConfigureCustomUserProperties method:

csharp b.Property<int>(nameof(AppUser.Reputation)).IsRequired().HasDefaultValue(1);

  1. Add new migration using Package Manager Console:

Add-Migration "Added_AppUser_Reputation"

This will produce a migration like this:

````csharp
public partial class Added_AppUser_Reputation : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
name: "Reputation",
table: "AbpUsers",
nullable: false,
defaultValue: 1);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "Reputation",
        table: "AbpUsers");
}

}
````

  1. Update the database schema:

Update-Database

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wocar picture wocar  路  3Comments

wocar picture wocar  路  3Comments

hikalkan picture hikalkan  路  3Comments

hitaspdotnet picture hitaspdotnet  路  3Comments

ugurozturk picture ugurozturk  路  3Comments