AuditLog.EntityFrameworkCore
0.4.1
dotnet add package AuditLog.EntityFrameworkCore --version 0.4.1
NuGet\Install-Package AuditLog.EntityFrameworkCore -Version 0.4.1
<PackageReference Include="AuditLog.EntityFrameworkCore" Version="0.4.1" />
<PackageVersion Include="AuditLog.EntityFrameworkCore" Version="0.4.1" />
<PackageReference Include="AuditLog.EntityFrameworkCore" />
paket add AuditLog.EntityFrameworkCore --version 0.4.1
#r "nuget: AuditLog.EntityFrameworkCore, 0.4.1"
#:package AuditLog.EntityFrameworkCore@0.4.1
#addin nuget:?package=AuditLog.EntityFrameworkCore&version=0.4.1
#tool nuget:?package=AuditLog.EntityFrameworkCore&version=0.4.1
AuditLog
Biblioteca de auditoria automática para EF Core com Source Generators Roslyn.
Pacotes
| Pacote | Descrição |
|---|---|
AuditLog.Abstractions |
Contratos: AuditConfigurator<T>, IAuditDescriptor, builders |
AuditLog.EntityFrameworkCore |
Integração EF Core: AuditSaveInterceptor, extensions |
AuditLog.Generator |
Source generator — gera *AuditLog, maps, descriptors |
AuditLog.EntityFrameworkCore.SoftDelete |
Runtime: interfaces, interceptor, query filters para soft delete |
AuditLog.Generator.SoftDelete |
Source generator — gera handlers tipados de cascade/restrict/set-null |
AuditLog — Auditoria de Entidades
1. Defina um configurador
[GenerateAuditLog]
public sealed class PacienteAuditConfigurator : AuditConfigurator<Paciente>
{
public PacienteAuditConfigurator()
{
For(x => x.Id).Key();
For(x => x.Nome).HasMaxLength(200).IsRequired();
For(x => x.Cpf).Sensitive().HasMaxLength(11);
For(x => x.DataAtualizacao).Ignore();
}
}
2. Adicione o interceptor no DbContext
public class AppDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new AuditSaveInterceptor());
}
}
3. O generator produz automaticamente
PacienteAuditLog— tabela de auditoria com snapshot dos dadosPacienteAuditLogDescriptor— mapeiaPaciente → PacienteAuditLogPacienteAuditLogEntityMap— EF Core configuration (column types, max length)ServiceCollectionExtensions.AddGeneratedAuditLogs()— DI registration
SoftDelete — Exclusão Lógica
Dois pacotes complementares:
| Package | Função |
|---|---|
AuditLog.EntityFrameworkCore.SoftDelete |
Runtime: interceptor, interfaces, query filters |
AuditLog.Generator.SoftDelete |
Source generator (opcional): gera handlers com tipagem forte |
Instalação
<ItemGroup>
<PackageReference Include="AuditLog.EntityFrameworkCore.SoftDelete" Version="1.0.0" />
<PackageReference Include="AuditLog.Generator.SoftDelete" Version="1.0.0" />
</ItemGroup>
1. Implemente ISoftDeleteEntity nas entidades
public class Paciente : ISoftDeleteEntity
{
public Guid Id { get; set; }
public string Nome { get; set; }
// Obrigatório para soft delete
public bool IsDeleted { get; set; }
public DateTime? DeletedAt { get; set; }
// Relacionamentos (Fluent API configurada no DbContext)
public List<Notificacao> Notificacoes { get; set; } = [];
}
2. Marque o DbContext com [GenerateSoftDelete]
using AuditLog.EntityFrameworkCore.SoftDelete;
[GenerateSoftDelete]
public class AppDbContext : DbContext
{
public DbSet<Paciente> Pacientes { get; set; }
public DbSet<Notificacao> Notificacoes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Paciente>(e =>
{
e.HasMany(x => x.Notificacoes)
.WithOne(x => x.Paciente)
.HasForeignKey(x => x.PacienteId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.ApplySoftDeleteQueryFilter();
}
}
3. Registre na DI
// Com o source generator (handlers tipados)
var registry = new SoftDeleteHandlerRegistry();
registry.AddGeneratedSoftDeleteHandlers();
services.AddSoftDelete(registry);
// Ou sem o generator (fallback via reflection)
services.AddSoftDelete();
4. Use normalmente
db.Pacientes.Remove(paciente);
await db.SaveChangesAsync();
// → IsDeleted = true, DeletedAt = now
// → Cascade: Notificacoes também são marcadas como deletadas
// → Query filter global: db.Pacientes retorna apenas não-deletados
Comportamentos por FK
OnDelete() |
Efeito |
|---|---|
Cascade |
Dependentes são soft-deletados recursivamente |
Restrict |
Lança RestrictDeleteViolationException se houver dependentes |
SetNull |
FK dos dependentes é setada como null |
Convenções (quando OnDelete não é especificado)
| Navigation | Comportamento |
|---|---|
Collection (List<T>) |
Cascade |
Reference (T) |
Restrict |
FK nullable (Guid?) |
SetNull |
Consultas
// Query filter automático — só não-deletados
db.Pacientes.ToList();
// Incluir deletados
db.Pacientes.IgnoreQueryFilters().ToList();
Suporte a herança indireta de IEntityTypeConfiguration<T>
O gerador detecta entity maps que implementam IEntityTypeConfiguration<T> através de toda a cadeia de herança, incluindo casos como AuditEntityMap<T> → IContextEntityMap<T> → IEntityTypeConfiguration<T>. Isso funciona tanto com ApplyConfiguration(new ConcreteEntityMap()) quanto com ApplyConfigurationsFromAssembly().
Descoberta de entidades sem DbSet<T>
O gerador descobre entidades mesmo quando o DbContext não possui propriedades DbSet<T>, desde que as entidades sejam registradas via modelBuilder.Entity<T>() no OnModelCreating ou via ApplyConfiguration/ApplyConfigurationsFromAssembly com entity maps que implementam IEntityTypeConfiguration<T>.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AuditLog.Abstractions (>= 0.4.1)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.