UgenTec.Framework.Core.EntityFramework QuickStart
The UgenTec.Framework.Core.EntityFramework
library contains helper classes for all things Entity Framework related.
Installation
Use the internal Ugentec Nuget-2 feed to install the UgenTec.Framework.Core.Http library
install-package UgenTec.Framework.Core.EntityFramework
Usage
Auditable entities
UgenTec.Framework.Core.EntityFramework contains the IAuditable
extension which can be implemented on application specific entities.
The ProcessAuditableEntries
extension method for DbContext can be used for automatically filling in auditing information.
public class MyEntity : IAuditable
{
public DateTime CreationDate { get; set; }
public string CreationUser { get; set; }
public DateTime ModificationDate { get; set; }
public string ModificationUser { get; set; }
}
public class TestDbContext : DbContext
{
private readonly string _userName;
public TestDbContext(DbContextOptions options, string userName) : base(options)
{
_userName = userName;
}
public override int SaveChanges()
{
this.ProcessAuditableEntries(() => _userName);
return base.SaveChanges();
}
public DbSet<MyEntity> TestRecords { get; set; }
}
Creating Table with SysId column with clustered index on it
public partial class IntroduceMultipleStandardCurves : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTableWithSysId("MyTable", "PK_MyTable", "Id");
}
}
Configuring EntityFrameworkCore for multitenancy where database connection is dependent on active tenant
Multitenancy depends on the presence of a ITenantIdentificationStrategy
and ITenantConnectionStringMapper
.
Framework core does not provide any concrete implementation for this as this logic is considered application specific.
To make use this extension, build implementations of ITenantIdentificationStrategy and ITenantConnectionStringMapper classes and register these in the service collection.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<FastFinderApiContext>((serviceProvider, options) =>
{
options.UseSqlServerWithMultiTenancy(
serviceProvider.GetRequiredService<ITenantIdentificationStrategy>(),
serviceProvider.GetRequiredService<ITenantConnectionStringMapper>());
};
}
Configuring code generator to generate migrations which add SysId column and convert PK index into non-clustered and SysId index into clustered index
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, MigrationCodeGeneratorWithGuidSupport>();
}
}