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)