From 283da6783b632bf5d00d834dd8f7b10c098a0b6c Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 26 Mar 2020 14:45:46 +0800 Subject: [PATCH 1/4] Apply Entity Extension System for samples Resolve #3361 --- .../Acme.BookStore.Domain/Users/AppUser.cs | 16 ++++++-- .../Acme.BookStore.Domain/Users/AppUser.cs | 16 ++++++-- .../BookStoreMigrationsDbContext.cs | 7 ---- .../BookStoreMigrationsDbContextFactory.cs | 2 + .../EntityFrameworkCore/BookStoreDbContext.cs | 8 ++-- ...okStoreDbContextModelCreatingExtensions.cs | 8 ---- .../BookStoreEntityExtensions.cs | 33 +++++++++++++++ .../BookStoreEntityFrameworkCoreModule.cs | 5 +++ .../BookManagementEntityExtensions.cs | 32 +++++++++++++++ ...BookManagementEntityFrameworkCoreModule.cs | 5 +++ .../Acme.BookStore.Domain/Users/AppUser.cs | 16 ++++++-- .../BookStoreMigrationsDbContext.cs | 7 ---- .../BookStoreMigrationsDbContextFactory.cs | 2 + .../EntityFrameworkCore/BookStoreDbContext.cs | 8 ++-- ...okStoreDbContextModelCreatingExtensions.cs | 8 ---- .../BookStoreEntityExtensions.cs | 33 +++++++++++++++ .../BookStoreEntityFrameworkCoreModule.cs | 5 +++ .../src/DashboardDemo.Domain/Users/AppUser.cs | 16 ++++++-- .../DashboardDemoMigrationsDbContext.cs | 7 ---- ...DashboardDemoMigrationsDbContextFactory.cs | 2 + .../DashboardDemoDbContext.cs | 8 ++-- ...ardDemoDbContextModelCreatingExtensions.cs | 8 ---- .../DashboardDemoEntityExtensions.cs | 33 +++++++++++++++ .../DashboardDemoEntityFrameworkCoreModule.cs | 5 +++ .../Acme.BookStore.Domain/Users/AppUser.cs | 16 ++++++-- .../BookStoreMigrationsDbContext.cs | 13 ------ .../BookStoreMigrationsDbContextFactory.cs | 2 + .../EntityFrameworkCore/BookStoreDbContext.cs | 8 ++-- ...okStoreDbContextModelCreatingExtensions.cs | 19 +-------- .../BookStoreEntityExtensions.cs | 40 +++++++++++++++++++ .../BookStoreEntityFrameworkCoreModule.cs | 5 +++ 31 files changed, 290 insertions(+), 103 deletions(-) create mode 100644 samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs create mode 100644 samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs create mode 100644 samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs create mode 100644 samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs create mode 100644 samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs index 9068cfbcd5..37f67ba60a 100644 --- a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -41,9 +41,19 @@ namespace Acme.BookStore.Users #endregion /* 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 BookStoreDbContext.OnModelCreating + * to configure the mapping for your new property + * 2. Update BookStoreEntityExtensions 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() { diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs index 9068cfbcd5..37f67ba60a 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -41,9 +41,19 @@ namespace Acme.BookStore.Users #endregion /* 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 BookStoreDbContext.OnModelCreating + * to configure the mapping for your new property + * 2. Update BookStoreEntityExtensions 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() { diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs index 6a74deef7e..7d7b1cb243 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs @@ -42,13 +42,6 @@ namespace Acme.BookStore.EntityFrameworkCore builder.ConfigureTenantManagement(); builder.ConfigureBookManagement(); - /* Configure customizations for entities from the modules included */ - - builder.Entity(b => - { - b.ConfigureCustomUserProperties(); - }); - /* Configure your own tables/entities inside the ConfigureBookStore method */ builder.ConfigureBookStore(); diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index 7212eb7340..4f285e597e 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,6 +11,8 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { + BookStoreEntityExtensions.Configure(); + var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder() diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index 85644bf40a..7ddeab7a5f 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -3,6 +3,7 @@ using Acme.BookStore.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; +using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; namespace Acme.BookStore.EntityFrameworkCore @@ -39,12 +40,13 @@ namespace Acme.BookStore.EntityFrameworkCore builder.Entity(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 BookStoreMigrationsDbContext class - b.ConfigureCustomUserProperties(); + /* Configure mappings for your additional properties + * Also see the BookStoreEntityExtensions class + */ }); /* Configure your own tables/entities inside the ConfigureBookStore method */ diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs index 1510809d5f..c132d297a4 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs @@ -1,7 +1,5 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; using Volo.Abp; -using Volo.Abp.Users; namespace Acme.BookStore.EntityFrameworkCore { @@ -20,11 +18,5 @@ namespace Acme.BookStore.EntityFrameworkCore // //... //}); } - - public static void ConfigureCustomUserProperties(this EntityTypeBuilder b) - where TUser: class, IUser - { - //b.Property(nameof(AppUser.MyProperty))... - } } } \ No newline at end of file diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs new file mode 100644 index 0000000000..771390698f --- /dev/null +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs @@ -0,0 +1,33 @@ +using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.Identity; +using Volo.Abp.Threading; + +namespace Acme.BookStore.EntityFrameworkCore +{ + public static class BookStoreEntityExtensions + { + 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( + * "MyProperty", + * b => + * { + * b.HasMaxLength(128); + * }); + * + * See the documentation for more: + * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities + */ + }); + } + } +} \ No newline at end of file diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index 5cb2a6dc7e..c8142b8088 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -29,6 +29,11 @@ namespace Acme.BookStore.EntityFrameworkCore )] public class BookStoreEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + BookStoreEntityExtensions.Configure(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => diff --git a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs new file mode 100644 index 0000000000..b7a3871453 --- /dev/null +++ b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs @@ -0,0 +1,32 @@ +using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.Threading; + +namespace Acme.BookStore.BookManagement.EntityFrameworkCore +{ + public static class BookManagementEntityExtensions + { + 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( + * "MyProperty", + * b => + * { + * b.HasMaxLength(128); + * }); + * + * See the documentation for more: + * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities + */ + }); + } + } +} \ No newline at end of file diff --git a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs index c2b2867938..3734551e73 100644 --- a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs +++ b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs @@ -10,6 +10,11 @@ namespace Acme.BookStore.BookManagement.EntityFrameworkCore )] public class BookManagementEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + BookManagementEntityExtensions.Configure(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => diff --git a/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs index 9068cfbcd5..37f67ba60a 100644 --- a/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -41,9 +41,19 @@ namespace Acme.BookStore.Users #endregion /* 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 BookStoreDbContext.OnModelCreating + * to configure the mapping for your new property + * 2. Update BookStoreEntityExtensions 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() { diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs index 541a91a0fd..5287d6f535 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs @@ -40,13 +40,6 @@ namespace Acme.BookStore.EntityFrameworkCore builder.ConfigureFeatureManagement(); builder.ConfigureTenantManagement(); - /* Configure customizations for entities from the modules included */ - - builder.Entity(b => - { - b.ConfigureCustomUserProperties(); - }); - /* Configure your own tables/entities inside the ConfigureBookStore method */ builder.ConfigureBookStore(); diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index da72e6fb21..cf8a18e468 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,6 +11,8 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { + BookStoreEntityExtensions.Configure(); + var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder() diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index 386eb2623b..5849e12137 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -3,6 +3,7 @@ using Acme.BookStore.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; +using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; namespace Acme.BookStore.EntityFrameworkCore @@ -41,15 +42,16 @@ namespace Acme.BookStore.EntityFrameworkCore builder.Entity(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.ConfigureFullAudited(); b.ConfigureExtraProperties(); b.ConfigureConcurrencyStamp(); b.ConfigureAbpUser(); - //Moved customization to a method so we can share it with the BookStoreMigrationsDbContext class - b.ConfigureCustomUserProperties(); + /* Configure mappings for your additional properties + * Also see the BookStoreEntityExtensions class + */ }); /* Configure your own tables/entities inside the ConfigureBookStore method */ diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs index 922793e029..f11ec0d00a 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs @@ -1,8 +1,6 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; using Volo.Abp; using Volo.Abp.EntityFrameworkCore.Modeling; -using Volo.Abp.Users; namespace Acme.BookStore.EntityFrameworkCore { @@ -21,11 +19,5 @@ namespace Acme.BookStore.EntityFrameworkCore b.Property(x => x.Name).IsRequired().HasMaxLength(128); }); } - - public static void ConfigureCustomUserProperties(this EntityTypeBuilder b) - where TUser: class, IUser - { - //b.Property(nameof(AppUser.MyProperty))... - } } } \ No newline at end of file diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs new file mode 100644 index 0000000000..771390698f --- /dev/null +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs @@ -0,0 +1,33 @@ +using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.Identity; +using Volo.Abp.Threading; + +namespace Acme.BookStore.EntityFrameworkCore +{ + public static class BookStoreEntityExtensions + { + 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( + * "MyProperty", + * b => + * { + * b.HasMaxLength(128); + * }); + * + * See the documentation for more: + * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities + */ + }); + } + } +} \ No newline at end of file diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index 156a4207cb..be5f6039b4 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -27,6 +27,11 @@ namespace Acme.BookStore.EntityFrameworkCore )] public class BookStoreEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + BookStoreEntityExtensions.Configure(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => diff --git a/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs b/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs index 2f2ba91689..cfad912abe 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs @@ -41,9 +41,19 @@ namespace DashboardDemo.Users #endregion /* 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 BookStoreDbContext.OnModelCreating + * to configure the mapping for your new property + * 2. Update BookStoreEntityExtensions 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() { diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContext.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContext.cs index c94d27faf0..ccec84b7e1 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContext.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContext.cs @@ -40,13 +40,6 @@ namespace DashboardDemo.EntityFrameworkCore builder.ConfigureFeatureManagement(); builder.ConfigureTenantManagement(); - /* Configure customizations for entities from the modules included */ - - builder.Entity(b => - { - b.ConfigureCustomUserProperties(); - }); - /* Configure your own tables/entities inside the ConfigureDashboardDemo method */ builder.ConfigureDashboardDemo(); diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs index 0773892abc..10ce7b7da1 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs @@ -11,6 +11,8 @@ namespace DashboardDemo.EntityFrameworkCore { public DashboardDemoMigrationsDbContext CreateDbContext(string[] args) { + DashboardDemoEntityExtensions.Configure(); + var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder() diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs index 94ec22abb3..33c1ddb82d 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs @@ -3,6 +3,7 @@ using DashboardDemo.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; +using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; namespace DashboardDemo.EntityFrameworkCore @@ -39,15 +40,16 @@ namespace DashboardDemo.EntityFrameworkCore builder.Entity(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.ConfigureFullAudited(); b.ConfigureExtraProperties(); b.ConfigureConcurrencyStamp(); b.ConfigureAbpUser(); - //Moved customization to a method so we can share it with the DashboardDemoMigrationsDbContext class - b.ConfigureCustomUserProperties(); + /* Configure mappings for your additional properties + * Also see the DashboardDemoEntityExtensions class + */ }); /* Configure your own tables/entities inside the ConfigureDashboardDemo method */ diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContextModelCreatingExtensions.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContextModelCreatingExtensions.cs index 60dae7c4cf..0a5d05f6bc 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContextModelCreatingExtensions.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContextModelCreatingExtensions.cs @@ -1,7 +1,5 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; using Volo.Abp; -using Volo.Abp.Users; namespace DashboardDemo.EntityFrameworkCore { @@ -20,11 +18,5 @@ namespace DashboardDemo.EntityFrameworkCore // //... //}); } - - public static void ConfigureCustomUserProperties(this EntityTypeBuilder b) - where TUser: class, IUser - { - //b.Property(nameof(AppUser.MyProperty))... - } } } \ No newline at end of file diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs new file mode 100644 index 0000000000..b41ccb8ac8 --- /dev/null +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs @@ -0,0 +1,33 @@ +using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.Identity; +using Volo.Abp.Threading; + +namespace DashboardDemo.EntityFrameworkCore +{ + public static class DashboardDemoEntityExtensions + { + 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( + * "MyProperty", + * b => + * { + * b.HasMaxLength(128); + * }); + * + * See the documentation for more: + * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities + */ + }); + } + } +} \ No newline at end of file diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs index de03726a20..b183c49446 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs @@ -27,6 +27,11 @@ namespace DashboardDemo.EntityFrameworkCore )] public class DashboardDemoEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + DashboardDemoEntityExtensions.Configure(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs index 9068cfbcd5..37f67ba60a 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -41,9 +41,19 @@ namespace Acme.BookStore.Users #endregion /* 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 BookStoreDbContext.OnModelCreating + * to configure the mapping for your new property + * 2. Update BookStoreEntityExtensions 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() { diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs index 7a83a323d8..8064243388 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs @@ -34,19 +34,6 @@ namespace Acme.BookStore.EntityFrameworkCore builder.ConfigureFeatureManagement(); builder.ConfigureTenantManagement(); - /* Configure customizations for entities from the modules included */ - - //CONFIGURE THE CUSTOM ROLE PROPERTIES - builder.Entity(b => - { - b.ConfigureCustomRoleProperties(); - }); - - builder.Entity(b => - { - b.ConfigureCustomUserProperties(); - }); - /* Configure your own tables/entities inside the ConfigureBookStore method */ builder.ConfigureBookStore(); diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index da72e6fb21..cf8a18e468 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,6 +11,8 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { + BookStoreEntityExtensions.Configure(); + var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder() diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index 091e5820fc..d3726865cd 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -4,6 +4,7 @@ using Acme.BookStore.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; +using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; namespace Acme.BookStore.EntityFrameworkCore @@ -51,12 +52,13 @@ namespace Acme.BookStore.EntityFrameworkCore builder.Entity(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 BookStoreMigrationsDbContext class - b.ConfigureCustomUserProperties(); + /* Configure mappings for your additional properties + * Also see the BookStoreEntityExtensions class + */ }); /* Configure your own tables/entities inside the ConfigureBookStore method */ diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs index 345bde14a0..c132d297a4 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs @@ -1,10 +1,5 @@ -using System; -using Acme.BookStore.Roles; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; using Volo.Abp; -using Volo.Abp.Domain.Entities; -using Volo.Abp.Users; namespace Acme.BookStore.EntityFrameworkCore { @@ -23,17 +18,5 @@ namespace Acme.BookStore.EntityFrameworkCore // //... //}); } - - public static void ConfigureCustomUserProperties(this EntityTypeBuilder b) - where TUser: class, IUser - { - //b.Property(nameof(AppUser.MyProperty))... - } - - public static void ConfigureCustomRoleProperties(this EntityTypeBuilder b) - where TRole : class, IEntity - { - b.Property(nameof(AppRole.Title)).HasMaxLength(128); - } } } \ No newline at end of file diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs new file mode 100644 index 0000000000..9d59942de8 --- /dev/null +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs @@ -0,0 +1,40 @@ +using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.Identity; +using Volo.Abp.Threading; + +namespace Acme.BookStore.EntityFrameworkCore +{ + public static class BookStoreEntityExtensions + { + 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( + * "MyProperty", + * b => + * { + * b.HasMaxLength(128); + * }); + * + * See the documentation for more: + * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities + */ + + EntityExtensionManager.AddProperty( + nameof(AppRole.Title), + b => + { + b.HasMaxLength(128); + }); + }); + } + } +} \ No newline at end of file diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index 156a4207cb..be5f6039b4 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -27,6 +27,11 @@ namespace Acme.BookStore.EntityFrameworkCore )] public class BookStoreEntityFrameworkCoreModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + BookStoreEntityExtensions.Configure(); + } + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => From 3d85fd01417a5e141c06847b90124e82f78868fc Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 26 Mar 2020 15:48:30 +0800 Subject: [PATCH 2/4] Undo changes to EfCoreMigrationDemo. --- build/common.ps1 | 1 + .../Acme.BookStore.Domain/Users/AppUser.cs | 16 ++------ .../BookStoreMigrationsDbContext.cs | 13 ++++++ .../BookStoreMigrationsDbContextFactory.cs | 3 +- .../EntityFrameworkCore/BookStoreDbContext.cs | 8 ++-- ...okStoreDbContextModelCreatingExtensions.cs | 19 ++++++++- .../BookStoreEntityExtensions.cs | 40 ------------------- .../BookStoreEntityFrameworkCoreModule.cs | 5 --- ...BookStore.EntityFrameworkCore.Tests.csproj | 1 + .../BookStoreEntityFrameworkCoreTestModule.cs | 12 +++++- .../BookStoreWebTestModule.cs | 8 ++++ 11 files changed, 59 insertions(+), 67 deletions(-) delete mode 100644 samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs diff --git a/build/common.ps1 b/build/common.ps1 index e4ba6a8adc..06fa20b35e 100644 --- a/build/common.ps1 +++ b/build/common.ps1 @@ -28,6 +28,7 @@ $solutionPaths = ( "../samples/BookStore-Modular/modules/book-management", "../samples/BookStore-Modular/application", "../samples/DashboardDemo", + "../samples/EfCoreMigrationDemo", "../samples/MicroserviceDemo", "../samples/RabbitMqEventBus", "../abp_io/AbpIoLocalization" diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs index 37f67ba60a..9068cfbcd5 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -41,19 +41,9 @@ namespace Acme.BookStore.Users #endregion /* Add your own properties here. Example: - * - * public string MyProperty { get; set; } - * - * If you add a property and using the EF Core, remember these; - * - * 1. update BookStoreDbContext.OnModelCreating - * to configure the mapping for your new property - * 2. Update BookStoreEntityExtensions 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. - */ + * + * public virtual string MyProperty { get; set; } + */ private AppUser() { diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs index 8064243388..7a83a323d8 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContext.cs @@ -34,6 +34,19 @@ namespace Acme.BookStore.EntityFrameworkCore builder.ConfigureFeatureManagement(); builder.ConfigureTenantManagement(); + /* Configure customizations for entities from the modules included */ + + //CONFIGURE THE CUSTOM ROLE PROPERTIES + builder.Entity(b => + { + b.ConfigureCustomRoleProperties(); + }); + + builder.Entity(b => + { + b.ConfigureCustomUserProperties(); + }); + /* Configure your own tables/entities inside the ConfigureBookStore method */ builder.ConfigureBookStore(); diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index cf8a18e468..7364cd29ab 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,8 +11,7 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { - BookStoreEntityExtensions.Configure(); - + var configuration = BuildConfiguration(); var builder = new DbContextOptionsBuilder() diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index d3726865cd..091e5820fc 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -4,7 +4,6 @@ using Acme.BookStore.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; -using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; namespace Acme.BookStore.EntityFrameworkCore @@ -52,13 +51,12 @@ namespace Acme.BookStore.EntityFrameworkCore builder.Entity(b => { - b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser + b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser b.ConfigureByConvention(); b.ConfigureAbpUser(); - /* Configure mappings for your additional properties - * Also see the BookStoreEntityExtensions class - */ + //Moved customization to a method so we can share it with the BookStoreMigrationsDbContext class + b.ConfigureCustomUserProperties(); }); /* Configure your own tables/entities inside the ConfigureBookStore method */ diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs index c132d297a4..345bde14a0 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs @@ -1,5 +1,10 @@ -using Microsoft.EntityFrameworkCore; +using System; +using Acme.BookStore.Roles; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; using Volo.Abp; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Users; namespace Acme.BookStore.EntityFrameworkCore { @@ -18,5 +23,17 @@ namespace Acme.BookStore.EntityFrameworkCore // //... //}); } + + public static void ConfigureCustomUserProperties(this EntityTypeBuilder b) + where TUser: class, IUser + { + //b.Property(nameof(AppUser.MyProperty))... + } + + public static void ConfigureCustomRoleProperties(this EntityTypeBuilder b) + where TRole : class, IEntity + { + b.Property(nameof(AppRole.Title)).HasMaxLength(128); + } } } \ No newline at end of file diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs deleted file mode 100644 index 9d59942de8..0000000000 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Volo.Abp.EntityFrameworkCore.Extensions; -using Volo.Abp.Identity; -using Volo.Abp.Threading; - -namespace Acme.BookStore.EntityFrameworkCore -{ - public static class BookStoreEntityExtensions - { - 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( - * "MyProperty", - * b => - * { - * b.HasMaxLength(128); - * }); - * - * See the documentation for more: - * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities - */ - - EntityExtensionManager.AddProperty( - nameof(AppRole.Title), - b => - { - b.HasMaxLength(128); - }); - }); - } - } -} \ No newline at end of file diff --git a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index be5f6039b4..156a4207cb 100644 --- a/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/EfCoreMigrationDemo/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -27,11 +27,6 @@ namespace Acme.BookStore.EntityFrameworkCore )] public class BookStoreEntityFrameworkCoreModule : AbpModule { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - BookStoreEntityExtensions.Configure(); - } - public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddAbpDbContext(options => diff --git a/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/Acme.BookStore.EntityFrameworkCore.Tests.csproj b/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/Acme.BookStore.EntityFrameworkCore.Tests.csproj index dd2137ad06..b03b0ed22d 100644 --- a/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/Acme.BookStore.EntityFrameworkCore.Tests.csproj +++ b/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/Acme.BookStore.EntityFrameworkCore.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/EntityFrameworkCore/BookStoreEntityFrameworkCoreTestModule.cs b/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/EntityFrameworkCore/BookStoreEntityFrameworkCoreTestModule.cs index e1171d6d90..a3f2bb5aa7 100644 --- a/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/EntityFrameworkCore/BookStoreEntityFrameworkCoreTestModule.cs +++ b/samples/EfCoreMigrationDemo/test/Acme.BookStore.EntityFrameworkCore.Tests/EntityFrameworkCore/BookStoreEntityFrameworkCoreTestModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Data.Sqlite; +using Acme.BookStore.DbMigrationsForSecondDb.EntityFrameworkCore; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; @@ -51,6 +52,15 @@ namespace Acme.BookStore.EntityFrameworkCore context.GetService().CreateTables(); } + var secondOptions = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new BookStoreSecondMigrationsDbContext(secondOptions)) + { + context.GetService().CreateTables(); + } + return connection; } } diff --git a/samples/EfCoreMigrationDemo/test/Acme.BookStore.Web.Tests/BookStoreWebTestModule.cs b/samples/EfCoreMigrationDemo/test/Acme.BookStore.Web.Tests/BookStoreWebTestModule.cs index 46e4312035..786015d18b 100644 --- a/samples/EfCoreMigrationDemo/test/Acme.BookStore.Web.Tests/BookStoreWebTestModule.cs +++ b/samples/EfCoreMigrationDemo/test/Acme.BookStore.Web.Tests/BookStoreWebTestModule.cs @@ -12,6 +12,7 @@ using Acme.BookStore.Web; using Acme.BookStore.Web.Menus; using Volo.Abp; using Volo.Abp.AspNetCore.TestBase; +using Volo.Abp.Data; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.UI.Navigation; @@ -38,6 +39,13 @@ namespace Acme.BookStore { ConfigureLocalizationServices(context.Services); ConfigureNavigationServices(context.Services); + + Configure(options => + { + //SqliteConnection does not support nested transactions. + //So Dbcontext and connectionString need be same. + options.ConnectionStrings.Clear(); + }); } private static void ConfigureLocalizationServices(IServiceCollection services) From d0ee68e612c7e362ba1108e9a5c57aacd47913f5 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 30 Mar 2020 10:33:20 +0800 Subject: [PATCH 3/4] Use object extension system. --- .../Acme.BookStore.Domain/Users/AppUser.cs | 4 ++-- .../Acme.BookStore.Domain/Users/AppUser.cs | 4 ++-- .../BookStoreMigrationsDbContextFactory.cs | 2 +- .../EntityFrameworkCore/BookStoreDbContext.cs | 2 +- .../BookStoreEntityExtensions.cs | 22 +++++++++++-------- .../BookStoreEntityFrameworkCoreModule.cs | 2 +- ...anagementEfCoreEntityExtensionMappings.cs} | 20 ++++++++++------- ...BookManagementEntityFrameworkCoreModule.cs | 2 +- .../Acme.BookStore.Domain/Users/AppUser.cs | 4 ++-- .../BookStoreMigrationsDbContextFactory.cs | 2 +- .../EntityFrameworkCore/BookStoreDbContext.cs | 2 +- ...BookStoreEfCoreEntityExtensionMappings.cs} | 22 +++++++++++-------- .../BookStoreEntityFrameworkCoreModule.cs | 2 +- .../src/DashboardDemo.Domain/Users/AppUser.cs | 4 ++-- ...DashboardDemoMigrationsDbContextFactory.cs | 2 +- .../DashboardDemoDbContext.cs | 2 +- ...boardDemoEfCoreEntityExtensionMappings.cs} | 22 +++++++++++-------- .../DashboardDemoEntityFrameworkCoreModule.cs | 2 +- .../Users/AppUser.cs | 2 +- 19 files changed, 70 insertions(+), 54 deletions(-) rename samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/{BookManagementEntityExtensions.cs => BookManagementEfCoreEntityExtensionMappings.cs} (51%) rename samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/{BookStoreEntityExtensions.cs => BookStoreEfCoreEntityExtensionMappings.cs} (50%) rename samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/{DashboardDemoEntityExtensions.cs => DashboardDemoEfCoreEntityExtensionMappings.cs} (50%) diff --git a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs index 37f67ba60a..b746e68052 100644 --- a/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore-Angular-MongoDb/aspnet-core/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -46,9 +46,9 @@ namespace Acme.BookStore.Users * * If you add a property and using the EF Core, remember these; * - * 1. update BookStoreDbContext.OnModelCreating + * 1. Update BookStoreDbContext.OnModelCreating * to configure the mapping for your new property - * 2. Update BookStoreEntityExtensions to extend the IdentityUser entity + * 2. Update BookStoreEfCoreEntityExtensionMappings 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 diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs index 37f67ba60a..b746e68052 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -46,9 +46,9 @@ namespace Acme.BookStore.Users * * If you add a property and using the EF Core, remember these; * - * 1. update BookStoreDbContext.OnModelCreating + * 1. Update BookStoreDbContext.OnModelCreating * to configure the mapping for your new property - * 2. Update BookStoreEntityExtensions to extend the IdentityUser entity + * 2. Update BookStoreEfCoreEntityExtensionMappings 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 diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index 4f285e597e..dad059f971 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,7 +11,7 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { - BookStoreEntityExtensions.Configure(); + BookStoreEfCoreEntityExtensionMappings.Configure(); var configuration = BuildConfiguration(); diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index 7ddeab7a5f..bfa602ef6b 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -45,7 +45,7 @@ namespace Acme.BookStore.EntityFrameworkCore b.ConfigureAbpUser(); /* Configure mappings for your additional properties - * Also see the BookStoreEntityExtensions class + * Also see the BookStoreEfCoreEntityExtensionMappings class */ }); diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs index 771390698f..cadfa73ccd 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs @@ -1,10 +1,10 @@ -using Volo.Abp.EntityFrameworkCore.Extensions; -using Volo.Abp.Identity; +using Volo.Abp.Identity; +using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; namespace Acme.BookStore.EntityFrameworkCore { - public static class BookStoreEntityExtensions + public static class BookStoreEfCoreEntityExtensionMappings { private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -15,14 +15,18 @@ namespace Acme.BookStore.EntityFrameworkCore /* You can configure entity extension properties for the * entities defined in the used modules. * + * The properties defined here becomes table fields. + * If you want to use the ExtraProperties dictionary of the entity + * instead of creating a new field, then define the property in the + * MyProjectNameDomainObjectExtensions class. + * * Example: * - * EntityExtensionManager.AddProperty( - * "MyProperty", - * b => - * { - * b.HasMaxLength(128); - * }); + * ObjectExtensionManager.Instance + * .MapEfCoreProperty( + * "MyProperty", + * b => b.HasMaxLength(128) + * ); * * See the documentation for more: * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index c8142b8088..58999b2b0d 100644 --- a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -31,7 +31,7 @@ namespace Acme.BookStore.EntityFrameworkCore { public override void PreConfigureServices(ServiceConfigurationContext context) { - BookStoreEntityExtensions.Configure(); + BookStoreEfCoreEntityExtensionMappings.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEfCoreEntityExtensionMappings.cs similarity index 51% rename from samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs rename to samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEfCoreEntityExtensionMappings.cs index b7a3871453..d4358d0b66 100644 --- a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityExtensions.cs +++ b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEfCoreEntityExtensionMappings.cs @@ -1,9 +1,9 @@ -using Volo.Abp.EntityFrameworkCore.Extensions; +using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; namespace Acme.BookStore.BookManagement.EntityFrameworkCore { - public static class BookManagementEntityExtensions + public static class BookManagementEfCoreEntityExtensionMappings { private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -14,14 +14,18 @@ namespace Acme.BookStore.BookManagement.EntityFrameworkCore /* You can configure entity extension properties for the * entities defined in the used modules. * + * The properties defined here becomes table fields. + * If you want to use the ExtraProperties dictionary of the entity + * instead of creating a new field, then define the property in the + * MyProjectNameDomainObjectExtensions class. + * * Example: * - * EntityExtensionManager.AddProperty( - * "MyProperty", - * b => - * { - * b.HasMaxLength(128); - * }); + * ObjectExtensionManager.Instance + * .MapEfCoreProperty( + * "MyProperty", + * b => b.HasMaxLength(128) + * ); * * See the documentation for more: * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities diff --git a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs index 3734551e73..758486bf7e 100644 --- a/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs +++ b/samples/BookStore-Modular/modules/book-management/src/Acme.BookStore.BookManagement.EntityFrameworkCore/EntityFrameworkCore/BookManagementEntityFrameworkCoreModule.cs @@ -12,7 +12,7 @@ namespace Acme.BookStore.BookManagement.EntityFrameworkCore { public override void PreConfigureServices(ServiceConfigurationContext context) { - BookManagementEntityExtensions.Configure(); + BookManagementEfCoreEntityExtensionMappings.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs b/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs index 37f67ba60a..b746e68052 100644 --- a/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs +++ b/samples/BookStore/src/Acme.BookStore.Domain/Users/AppUser.cs @@ -46,9 +46,9 @@ namespace Acme.BookStore.Users * * If you add a property and using the EF Core, remember these; * - * 1. update BookStoreDbContext.OnModelCreating + * 1. Update BookStoreDbContext.OnModelCreating * to configure the mapping for your new property - * 2. Update BookStoreEntityExtensions to extend the IdentityUser entity + * 2. Update BookStoreEfCoreEntityExtensionMappings 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 diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs index cf8a18e468..52307142f4 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/BookStoreMigrationsDbContextFactory.cs @@ -11,7 +11,7 @@ namespace Acme.BookStore.EntityFrameworkCore { public BookStoreMigrationsDbContext CreateDbContext(string[] args) { - BookStoreEntityExtensions.Configure(); + BookStoreEfCoreEntityExtensionMappings.Configure(); var configuration = BuildConfiguration(); diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs index 5849e12137..c2df7ee6d0 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs @@ -50,7 +50,7 @@ namespace Acme.BookStore.EntityFrameworkCore b.ConfigureAbpUser(); /* Configure mappings for your additional properties - * Also see the BookStoreEntityExtensions class + * Also see the BookStoreEfCoreEntityExtensionMappings class */ }); diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEfCoreEntityExtensionMappings.cs similarity index 50% rename from samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs rename to samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEfCoreEntityExtensionMappings.cs index 771390698f..cadfa73ccd 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEfCoreEntityExtensionMappings.cs @@ -1,10 +1,10 @@ -using Volo.Abp.EntityFrameworkCore.Extensions; -using Volo.Abp.Identity; +using Volo.Abp.Identity; +using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; namespace Acme.BookStore.EntityFrameworkCore { - public static class BookStoreEntityExtensions + public static class BookStoreEfCoreEntityExtensionMappings { private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -15,14 +15,18 @@ namespace Acme.BookStore.EntityFrameworkCore /* You can configure entity extension properties for the * entities defined in the used modules. * + * The properties defined here becomes table fields. + * If you want to use the ExtraProperties dictionary of the entity + * instead of creating a new field, then define the property in the + * MyProjectNameDomainObjectExtensions class. + * * Example: * - * EntityExtensionManager.AddProperty( - * "MyProperty", - * b => - * { - * b.HasMaxLength(128); - * }); + * ObjectExtensionManager.Instance + * .MapEfCoreProperty( + * "MyProperty", + * b => b.HasMaxLength(128) + * ); * * See the documentation for more: * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities diff --git a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs index be5f6039b4..3faeb576da 100644 --- a/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs +++ b/samples/BookStore/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityFrameworkCoreModule.cs @@ -29,7 +29,7 @@ namespace Acme.BookStore.EntityFrameworkCore { public override void PreConfigureServices(ServiceConfigurationContext context) { - BookStoreEntityExtensions.Configure(); + BookStoreEfCoreEntityExtensionMappings.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs b/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs index cfad912abe..43f40fed20 100644 --- a/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs +++ b/samples/DashboardDemo/src/DashboardDemo.Domain/Users/AppUser.cs @@ -46,9 +46,9 @@ namespace DashboardDemo.Users * * If you add a property and using the EF Core, remember these; * - * 1. update BookStoreDbContext.OnModelCreating + * 1. Update BookStoreDbContext.OnModelCreating * to configure the mapping for your new property - * 2. Update BookStoreEntityExtensions to extend the IdentityUser entity + * 2. Update DashboardDemoEfCoreEntityExtensionMappings 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 diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs index 10ce7b7da1..b9f1526ad4 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore.DbMigrations/EntityFrameworkCore/DashboardDemoMigrationsDbContextFactory.cs @@ -11,7 +11,7 @@ namespace DashboardDemo.EntityFrameworkCore { public DashboardDemoMigrationsDbContext CreateDbContext(string[] args) { - DashboardDemoEntityExtensions.Configure(); + DashboardDemoEfCoreEntityExtensionMappings.Configure(); var configuration = BuildConfiguration(); diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs index 33c1ddb82d..a5d0bdea2e 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoDbContext.cs @@ -48,7 +48,7 @@ namespace DashboardDemo.EntityFrameworkCore b.ConfigureAbpUser(); /* Configure mappings for your additional properties - * Also see the DashboardDemoEntityExtensions class + * Also see the DashboardDemoEfCoreEntityExtensionMappings class */ }); diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEfCoreEntityExtensionMappings.cs similarity index 50% rename from samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs rename to samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEfCoreEntityExtensionMappings.cs index b41ccb8ac8..2e5ce4d0bf 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityExtensions.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEfCoreEntityExtensionMappings.cs @@ -1,10 +1,10 @@ -using Volo.Abp.EntityFrameworkCore.Extensions; -using Volo.Abp.Identity; +using Volo.Abp.Identity; +using Volo.Abp.ObjectExtending; using Volo.Abp.Threading; namespace DashboardDemo.EntityFrameworkCore { - public static class DashboardDemoEntityExtensions + public static class DashboardDemoEfCoreEntityExtensionMappings { private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); @@ -15,14 +15,18 @@ namespace DashboardDemo.EntityFrameworkCore /* You can configure entity extension properties for the * entities defined in the used modules. * + * The properties defined here becomes table fields. + * If you want to use the ExtraProperties dictionary of the entity + * instead of creating a new field, then define the property in the + * MyProjectNameDomainObjectExtensions class. + * * Example: * - * EntityExtensionManager.AddProperty( - * "MyProperty", - * b => - * { - * b.HasMaxLength(128); - * }); + * ObjectExtensionManager.Instance + * .MapEfCoreProperty( + * "MyProperty", + * b => b.HasMaxLength(128) + * ); * * See the documentation for more: * https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities diff --git a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs index b183c49446..043cf10197 100644 --- a/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs +++ b/samples/DashboardDemo/src/DashboardDemo.EntityFrameworkCore/EntityFrameworkCore/DashboardDemoEntityFrameworkCoreModule.cs @@ -29,7 +29,7 @@ namespace DashboardDemo.EntityFrameworkCore { public override void PreConfigureServices(ServiceConfigurationContext context) { - DashboardDemoEntityExtensions.Configure(); + DashboardDemoEfCoreEntityExtensionMappings.Configure(); } public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs index cd5c8dbb3d..1b5b3996ee 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Users/AppUser.cs @@ -46,7 +46,7 @@ namespace MyCompanyName.MyProjectName.Users * * If you add a property and using the EF Core, remember these; * - * 1. update MyProjectNameDbContext.OnModelCreating + * 1. Update MyProjectNameDbContext.OnModelCreating * to configure the mapping for your new property * 2. Update MyProjectNameEfCoreEntityExtensionMappings to extend the IdentityUser entity * and add your new property to the migration. From 4a83c2db832b698f478074290fb20b1687699abf Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 30 Mar 2020 10:39:07 +0800 Subject: [PATCH 4/4] Rename BookStoreEntityExtensions to BookStoreEfCoreEntityExtensionMappings. --- ...ityExtensions.cs => BookStoreEfCoreEntityExtensionMappings.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/{BookStoreEntityExtensions.cs => BookStoreEfCoreEntityExtensionMappings.cs} (100%) diff --git a/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs b/samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEfCoreEntityExtensionMappings.cs similarity index 100% rename from samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEntityExtensions.cs rename to samples/BookStore-Modular/application/src/Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreEfCoreEntityExtensionMappings.cs