using Microsoft.EntityFrameworkCore;
using CredentialManager.Models;
namespace CredentialManager.Data;
///
/// DbContext per la gestione delle credenziali
///
public class CredentialDbContext : DbContext
{
public DbSet Credentials { get; set; }
public DbSet RecordAssociations { get; set; }
public CredentialDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // Configurazione della tabella Credentials
modelBuilder.Entity(entity =>
{
entity.ToTable("Credentials");
entity.HasKey(e => e.Id);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(100);
entity.Property(e => e.Type)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.DatabaseType)
.HasMaxLength(50);
entity.Property(e => e.ConnectionString)
.HasMaxLength(500);
entity.Property(e => e.Host)
.HasMaxLength(200);
entity.Property(e => e.DatabaseName)
.HasMaxLength(100);
entity.Property(e => e.Username)
.HasMaxLength(100);
entity.Property(e => e.EncryptedApiKey)
.HasMaxLength(500);
entity.Property(e => e.EncryptedAuthToken)
.HasMaxLength(500);
entity.Property(e => e.Headers)
.HasMaxLength(2000);
entity.Property(e => e.AdditionalParameters)
.HasMaxLength(2000);
entity.Property(e => e.CreatedBy)
.HasMaxLength(100);
// Valori di default
entity.Property(e => e.CommandTimeout)
.HasDefaultValue(30);
entity.Property(e => e.TimeoutSeconds)
.HasDefaultValue(100);
entity.Property(e => e.IgnoreSslErrors)
.HasDefaultValue(false);
entity.Property(e => e.IsActive)
.HasDefaultValue(true);
// Indici
entity.HasIndex(e => e.Name)
.IsUnique();
entity.HasIndex(e => e.Type);
entity.HasIndex(e => e.DatabaseType);
entity.HasIndex(e => e.IsActive);
});
// Configurazione della tabella RecordAssociations
modelBuilder.Entity(entity =>
{
entity.ToTable("RecordAssociations");
entity.HasKey(e => e.Id);
entity.Property(e => e.SourceName)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.SourceType)
.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.SourceKey)
.IsRequired()
.HasMaxLength(500);
entity.Property(e => e.DestinationEntity)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.DestinationId)
.IsRequired()
.HasMaxLength(200);
entity.Property(e => e.RestCredentialName)
.IsRequired()
.HasMaxLength(100);
entity.Property(e => e.AdditionalInfo)
.HasMaxLength(2000);
// Valori di default
entity.Property(e => e.IsActive)
.HasDefaultValue(true);
// Indici
entity.HasIndex(e => new { e.SourceName, e.SourceKey, e.DestinationEntity })
.IsUnique()
.HasDatabaseName("IX_RecordAssociations_Unique");
entity.HasIndex(e => e.SourceType);
entity.HasIndex(e => e.DestinationEntity);
entity.HasIndex(e => e.RestCredentialName);
entity.HasIndex(e => e.IsActive);
entity.HasIndex(e => e.CreatedAt);
});
}
}