418.6 Creating LpPhbkDbContext for Lookup Resource Entities of ABP framework applications - chempkovsky/CS82ANGULAR GitHub Wiki

  • in the EntityFrameworkCore-folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project create LpPhbkDbContext-class as shown below:
Click to show the code
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace rupbes.firstapp.EntityFrameworkCore
{
    [ConnectionStringName("LpphbkConnStr")]
    public class LpPhbkDbContext : AbpDbContext<LpPhbkDbContext>
    {
        public LpPhbkDbContext(DbContextOptions<LpPhbkDbContext> options) : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
        }
    }
}
  • in the EntityFrameworkCore-folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project create LpPhbkDbContextCreatingExtensions-class as shown below:
Click to show the code
using Microsoft.EntityFrameworkCore;
using rupbes.firstapp.LpPhBk;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace rupbes.firstapp.EntityFrameworkCore
{
    public static class LpPhbkDbContextCreatingExtensions
    {
        public static void ConfigureLpPhbkDbContext(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

        }
    }
}
  • in the EntityFrameworkCore-folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project create LpPhbkDbContextFactory-class as shown below:
Click to show the code
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace rupbes.firstapp.EntityFrameworkCore
{
    internal class LpPhbkDbContextFactory : IDesignTimeDbContextFactory<LpPhbkDbContext>
    {
        public LpPhbkDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            firstappEfCoreEntityExtensionMappings.Configure();

            var builder = new DbContextOptionsBuilder<LpPhbkDbContext>()
                .UseSqlServer(configuration.GetConnectionString("LpphbkConnStr"));

            return new LpPhbkDbContext(builder.Options);
        }
        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../rupbes.firstapp.DbMigrator/"))
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
  • make sure Directory.Build.props looks like below
<Project>
    <PropertyGroup>
        <DefineConstants>NOTMODELING</DefineConstants>
    </PropertyGroup>
</Project>
  • in the EntityFrameworkCore-folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project create firstappEntityFrameworkCoreModule-class as shown below:
Click to show the code
[DependsOn(
    typeof(firstappDomainModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),
    typeof(AbpEntityFrameworkCoreSqlServerModule),
    typeof(AbpBackgroundJobsEntityFrameworkCoreModule),
    typeof(AbpAuditLoggingEntityFrameworkCoreModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpOpenIddictEntityFrameworkCoreModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule),
    typeof(BlobStoringDatabaseEntityFrameworkCoreModule)
    )]
public class firstappEntityFrameworkCoreModule : AbpModule
{

...
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
...
        context.Services.AddAbpDbContext<secondDbContext>();
        context.Services.AddAbpDbContext<LpPhbkDbContext>();
...
    }
...
}
  • modify appsettings.json of the rupbes.firstapp.HttpApi.Host.csproj and rupbes.firstapp.DbMigrator.csproj projects as shown below
{
  ...
  "ConnectionStrings": {
    ...
    "LpphbkConnStr": "Server=(LocalDb)\\MSSQLLocalDB;Database=lpphbk;Trusted_Connection=True;TrustServerCertificate=true"
    ...
  }
  ...
}
  • In the command line terminal make the folder of the rupbes.firstapp.EntityFrameworkCore.csproj-project to be active
    • run ef migrations for the LpPhbkDbContext-Dbcontext
D:\Development\rupbes.firstapp\src\rupbes.firstapp.EntityFrameworkCore>dotnet ef migrations add Initial --context LpPhbkDbContext --output-dir LpPhbkDbMigrations
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
⚠️ **GitHub.com Fallback** ⚠️