This is fifth post of the series: .NET Microservice with ABP
Posts in the Series
Part 3. Administration Service
Part 5. SaaS Service (this post)
Part 8. Identity server and Angular App
Table of contents
- Intro
- Add the shared project as a reference to the host
- Update the connection string
- Update the
SaaSServiceHttpApiHostModule
- Create the
DbContextFactory
in the EntityFrameworkCore project - Update the
Tasky.SaaSService.EntityFrameworkCore
- Migration
Intro
In this post we will see how to setup SaaS service. we only have tenant management module in this service.
Add the shared project as a reference to the host
1<ProjectReference Include="..\..\..\..\shared\Tasky.Shared.Hosting\Tasky.Shared.Hosting.csproj" />
Update the connection string
User ID=postgres;Password=postgres;Host=localhost;Port=5432;Database=TaskySaaSService;Pooling=false;
SaaSServiceHttpApiHostModule
Update the Update the depends on.
1[DependsOn( 2 typeof(TaskyHostingModule), 3 typeof(SaaSServiceApplicationModule), 4 typeof(SaaSServiceEntityFrameworkCoreModule), 5 typeof(SaaSServiceHttpApiModule), 6)]
Remove the things which are configured in the shared project.
DbContextFactory
in the EntityFrameworkCore project
Create the 1using System.IO; 2using Microsoft.EntityFrameworkCore; 3using Microsoft.EntityFrameworkCore.Design; 4using Microsoft.Extensions.Configuration; 5 6namespace Tasky.SaaSService.EntityFrameworkCore; 7 8public class SaaSServiceDbContextFactory : IDesignTimeDbContextFactory<SaaSServiceDbContext> 9{ 10 public SaaSServiceDbContext CreateDbContext(string[] args) 11 { 12 var builder = new DbContextOptionsBuilder<SaaSServiceDbContext>() 13 .UseNpgsql(GetConnectionStringFromConfiguration()); 14 15 return new SaaSServiceDbContext(builder.Options); 16 } 17 18 private static string GetConnectionStringFromConfiguration() 19 { 20 return BuildConfiguration() 21 .GetConnectionString(SaaSServiceDbProperties.ConnectionStringName); 22 } 23 24 private static IConfigurationRoot BuildConfiguration() 25 { 26 var builder = new ConfigurationBuilder() 27 .SetBasePath( 28 Path.Combine( 29 Directory.GetParent(Directory.GetCurrentDirectory())?.Parent!.FullName!, 30 $"host{Path.DirectorySeparatorChar}Tasky.SaaSService.HttpApi.Host" 31 ) 32 ) 33 .AddJsonFile("appsettings.json", false); 34 35 return builder.Build(); 36 } 37}
Tasky.SaaSService.EntityFrameworkCore
Update the Update the nuget packages
1<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="5.1.4"/> 2<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="5.1.4"/> 3<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1"/>
Update the SaaSServiceDbContext
1using Microsoft.EntityFrameworkCore; 2using Volo.Abp.Data; 3using Volo.Abp.EntityFrameworkCore; 4using Volo.Abp.TenantManagement; 5using Volo.Abp.TenantManagement.EntityFrameworkCore; 6 7namespace Tasky.SaaSService.EntityFrameworkCore; 8 9[ConnectionStringName(SaaSServiceDbProperties.ConnectionStringName)] 10public class SaaSServiceDbContext : AbpDbContext<SaaSServiceDbContext>, ITenantManagementDbContext, ISaaSServiceDbContext 11{ 12 public SaaSServiceDbContext(DbContextOptions<SaaSDbContext> options) 13 : base(options) 14 { 15 } 16 17 public DbSet<Tenant> Tenants { get; set; } 18 19 public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; } 20 21 protected override void OnModelCreating(ModelBuilder builder) 22 { 23 base.OnModelCreating(builder); 24 25 builder.ConfigureSaaSService(); 26 builder.ConfigureTenantManagement(); 27 } 28}
Update the SaaSServiceEntityFrameworkCoreModule
1using System; 2using Microsoft.Extensions.DependencyInjection; 3using Volo.Abp.EntityFrameworkCore; 4using Volo.Abp.Modularity; 5using Volo.Abp.TenantManagement.EntityFrameworkCore; 6 7namespace Tasky.SaaSService.EntityFrameworkCore; 8 9[DependsOn( 10 typeof(SaaSServiceDomainModule), 11 typeof(AbpTenantManagementEntityFrameworkCoreModule), 12 typeof(AbpEntityFrameworkCoreModule), 13 typeof(AbpEntityFrameworkCorePostgreSqlModule) 14)] 15public class SaaSServiceEntityFrameworkCoreModule : AbpModule 16{ 17 public override void ConfigureServices(ServiceConfigurationContext context) 18 { 19 Configure<AbpDbContextOptions>(options => 20 { 21 options.UseNpgsql(); 22 }); 23 24 AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 25 26 context.Services.AddAbpDbContext<SaaSDbContext>(options => 27 { 28 options.ReplaceDbContext<ITenantManagementDbContext>(); 29 options.AddDefaultRepositories(true); 30 }); 31 } 32}
Once this is created delete EntityFrameworkCore
folder can be created.
Migration
To create migrations
dotnet ef migrations add Init
To update database
dotnet ef database update