- Published on
SaaS Services
Series: .NET Microservice with ABP
This post is part of the .NET Microservice with ABP series.
Table of Contents
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
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;
Update the SaaSServiceHttpApiHostModule
Update the depends on.
[DependsOn(
typeof(TaskyHostingModule),
typeof(SaaSServiceApplicationModule),
typeof(SaaSServiceEntityFrameworkCoreModule),
typeof(SaaSServiceHttpApiModule),
)]
Remove the things which are configured in the shared project.
Create the DbContextFactory in the EntityFrameworkCore project
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();
}
}
Update the Tasky.SaaSService.EntityFrameworkCore
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
Related Posts
Continue reading with these related articles
Migrating Tye to Aspire
In this post we will see how to migrate the Tye to Aspire
.Net Microservice template with ABP
In this post I will show you how to create ABP microservice using a dotnet new template.
Migrating Identity Service to OpenIddict Module
In this post we will see how to replace Identity server with OpenIddict in our microservice