Browse Source

Update startup template to use the new entity extension system to customize the IdentityUser entity

pull/3360/head
Halil İbrahim Kalkan 7 years ago
parent
commit
fc3ae6a190
  1. 12
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs
  2. 7
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/MyProjectNameMigrationsDbContext.cs
  3. 2
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/MyProjectNameMigrationsDbContextFactory.cs
  4. 9
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameDbContext.cs
  5. 8
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameDbContextModelCreatingExtensions.cs
  6. 33
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEntityExtensions.cs
  7. 5
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEntityFrameworkCoreModule.cs

12
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs

@ -42,7 +42,17 @@ namespace MyCompanyName.MyProjectName.Users
/* Add your own properties here. Example:
*
* public virtual string MyProperty { get; set; }
* public string MyProperty { get; set; }
*
* If you add a property and using the EF Core, remember these;
*
* 1. update MyProjectNameDbContext.OnModelCreating
* to configure the mapping for your new property
* 2. Update MyProjectNameEntityExtensions to extend the IdentityUser entity
* and add your new property to the migration.
* 3. Use the Add-Migration to add a new database migration.
* 4. Run the .DbMigrator project (or use the Update-Database command) to apply
* schema change to the database.
*/
private AppUser()

7
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/MyProjectNameMigrationsDbContext.cs

@ -40,13 +40,6 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
builder.ConfigureFeatureManagement();
builder.ConfigureTenantManagement();
/* Configure customizations for entities from the modules included */
builder.Entity<IdentityUser>(b =>
{
b.ConfigureCustomUserProperties();
});
/* Configure your own tables/entities inside the ConfigureMyProjectName method */
builder.ConfigureMyProjectName();

2
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/MyProjectNameMigrationsDbContextFactory.cs

@ -11,6 +11,8 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
public MyProjectNameMigrationsDbContext CreateDbContext(string[] args)
{
MyProjectNameEntityExtensions.Configure();
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<MyProjectNameMigrationsDbContext>()

9
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameDbContext.cs

@ -3,6 +3,7 @@ using MyCompanyName.MyProjectName.Users;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Identity;
using Volo.Abp.Users.EntityFrameworkCore;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
@ -39,12 +40,14 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
builder.Entity<AppUser>(b =>
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureByConvention();
b.ConfigureAbpUser();
//Moved customization to a method so we can share it with the MyProjectNameMigrationsDbContext class
b.ConfigureCustomUserProperties();
/* Configure mappings for your additional properties
* Also see the MyProjectNameEntityExtensions class
*/
});
/* Configure your own tables/entities inside the ConfigureMyProjectName method */

8
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameDbContextModelCreatingExtensions.cs

@ -1,7 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp;
using Volo.Abp.Users;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
@ -20,11 +18,5 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
// //...
//});
}
public static void ConfigureCustomUserProperties<TUser>(this EntityTypeBuilder<TUser> b)
where TUser: class, IUser
{
//b.Property<string>(nameof(AppUser.MyProperty))...
}
}
}

33
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEntityExtensions.cs

@ -0,0 +1,33 @@
using Volo.Abp.EntityFrameworkCore.Extensions;
using Volo.Abp.Identity;
using Volo.Abp.Threading;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore
{
public static class MyProjectNameEntityExtensions
{
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
public static void Configure()
{
OneTimeRunner.Run(() =>
{
/* You can configure entity extension properties for the
* entities defined in the used modules.
*
* Example:
*
* EntityExtensionManager.AddProperty<IdentityUser, string>(
* "MyProperty",
* b =>
* {
* b.HasMaxLength(128);
* });
*
* See the documentation for more:
* https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities
*/
});
}
}
}

5
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/EntityFrameworkCore/MyProjectNameEntityFrameworkCoreModule.cs

@ -27,6 +27,11 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore
)]
public class MyProjectNameEntityFrameworkCoreModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
MyProjectNameEntityExtensions.Configure();
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<MyProjectNameDbContext>(options =>

Loading…
Cancel
Save