Switch Owned value types to ComplexProperty which is better fit.
This requires fluent configuration. Also made some conversion registration context wide.
This commit is contained in:
parent
07393f57fc
commit
1a8c63808a
11 changed files with 93 additions and 54 deletions
|
|
@ -1,76 +0,0 @@
|
|||
using IdentityShroud.Core.Model;
|
||||
using IdentityShroud.Core.Security;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace IdentityShroud.Core;
|
||||
|
||||
public class DbConfiguration
|
||||
{
|
||||
public string ConnectionString { get; set; } = "";
|
||||
public bool LogSensitiveData { get; set; } = false;
|
||||
}
|
||||
|
||||
public class Db(
|
||||
IOptions<DbConfiguration> configuration,
|
||||
ILoggerFactory? loggerFactory)
|
||||
: DbContext
|
||||
{
|
||||
public virtual DbSet<Client> Clients { get; set; }
|
||||
public virtual DbSet<Realm> Realms { get; set; }
|
||||
public virtual DbSet<RealmKey> Keys { get; set; }
|
||||
public virtual DbSet<RealmDek> Deks { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var dekIdConverter = new ValueConverter<DekId, Guid>(
|
||||
id => id.Id,
|
||||
guid => new DekId(guid));
|
||||
|
||||
var kekIdConverter = new ValueConverter<KekId, Guid>(
|
||||
id => id.Id,
|
||||
guid => new KekId(guid));
|
||||
|
||||
modelBuilder.Entity<RealmDek>()
|
||||
.Property(d => d.Id)
|
||||
.HasConversion(dekIdConverter);
|
||||
|
||||
modelBuilder.Entity<RealmDek>()
|
||||
.OwnsOne(d => d.KeyData, keyData =>
|
||||
{
|
||||
keyData.Property(k => k.KekId).HasConversion(kekIdConverter);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<RealmKey>()
|
||||
.OwnsOne(k => k.Key, key =>
|
||||
{
|
||||
key.Property(k => k.KekId).HasConversion(kekIdConverter);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<ClientSecret>()
|
||||
.OwnsOne(c => c.Secret, secret =>
|
||||
{
|
||||
secret.Property(s => s.DekId).HasConversion(dekIdConverter);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("<connection string>");
|
||||
optionsBuilder.UseNpgsql(
|
||||
configuration.Value.ConnectionString,
|
||||
o => o.MigrationsAssembly("IdentityShroud.Migrations")); // , o => o.UseNodaTime().UseVector().MigrationsAssembly("Migrations.KnowledgeBaseDB"));
|
||||
optionsBuilder.UseSnakeCaseNamingConvention();
|
||||
|
||||
if (configuration.Value.LogSensitiveData)
|
||||
optionsBuilder.EnableSensitiveDataLogging();
|
||||
|
||||
if (loggerFactory is { } )
|
||||
{
|
||||
optionsBuilder.UseLoggerFactory(loggerFactory);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue