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
<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.
[DependsOn(
typeof(TaskyHostingModule),
typeof(SaaSServiceApplicationModule),
typeof(SaaSServiceEntityFrameworkCoreModule),
typeof(SaaSServiceHttpApiModule),
)]
Remove the things which are configured in the shared project.
DbContextFactory
in the EntityFrameworkCore project
Create the using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace Tasky.SaaSService.EntityFrameworkCore;
public class SaaSServiceDbContextFactory : IDesignTimeDbContextFactory<SaaSServiceDbContext>
{
public SaaSServiceDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<SaaSServiceDbContext>()
.UseNpgsql(GetConnectionStringFromConfiguration());
return new SaaSServiceDbContext(builder.Options);
}
private static string GetConnectionStringFromConfiguration()
{
return BuildConfiguration()
.GetConnectionString(SaaSServiceDbProperties.ConnectionStringName);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(
Path.Combine(
Directory.GetParent(Directory.GetCurrentDirectory())?.Parent!.FullName!,
$"host{Path.DirectorySeparatorChar}Tasky.SaaSService.HttpApi.Host"
)
)
.AddJsonFile("appsettings.json", false);
return builder.Build();
}
}
Tasky.SaaSService.EntityFrameworkCore
Update the Update the nuget packages
<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="5.1.4"/>
<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="5.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1"/>
Update the SaaSServiceDbContext
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
namespace Tasky.SaaSService.EntityFrameworkCore;
[ConnectionStringName(SaaSServiceDbProperties.ConnectionStringName)]
public class SaaSServiceDbContext : AbpDbContext<SaaSServiceDbContext>, ITenantManagementDbContext, ISaaSServiceDbContext
{
public SaaSServiceDbContext(DbContextOptions<SaaSDbContext> options)
: base(options)
{
}
public DbSet<Tenant> Tenants { get; set; }
public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ConfigureSaaSService();
builder.ConfigureTenantManagement();
}
}
Update the SaaSServiceEntityFrameworkCoreModule
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
namespace Tasky.SaaSService.EntityFrameworkCore;
[DependsOn(
typeof(SaaSServiceDomainModule),
typeof(AbpTenantManagementEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCorePostgreSqlModule)
)]
public class SaaSServiceEntityFrameworkCoreModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpDbContextOptions>(options =>
{
options.UseNpgsql();
});
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
context.Services.AddAbpDbContext<SaaSDbContext>(options =>
{
options.ReplaceDbContext<ITenantManagementDbContext>();
options.AddDefaultRepositories(true);
});
}
}
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