PM> Install-Package Canducci.SoftDelete
Declare o namespace using Canducci.SoftDelete;
and implementation class
, example:
public class People: ISoftDeleteChar
{
...
public char DeletedAt { get; } = 'N';
}
public class People: ISoftDeleteBool
{
...
public bool DeletedAt { get; } = false;
}
public class Animal: ISoftDeleteDateTime
{
...
public DateTime? DeletedAt { get; } = null;
}
In the configuration em DbContext
, configure AddInterceptorSoftDelete
is method extension:
.AddInterceptorSoftDeleteChar() // Char
.AddInterceptorSoftDeleteBool() // Bool
.AddInterceptorSoftDeleteDateTime(); // DateTime?
and configure HasQueryFilterSoftDelete
is method extension:
options.HasQueryFilterSoftDeleteChar(); //Char
options.HasQueryFilterSoftDeleteBool(); //Bool
options.HasQueryFilterSoftDeleteDateTime(); //DateTime?
public class DatabaseContext : DbContext
{
public DbSet<Animal> Animal { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source = db.db", options =>
{
})
.AddInterceptorSoftDeleteChar() // Char
.AddInterceptorSoftDeleteBool() // Bool
.AddInterceptorSoftDeleteDateTime(); // DateTime?
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Animal>(options =>
{
options.ToTable("animal");
options.HasKey(x => x.Id);
options.Property(x => x.Id)
.HasColumnName("id");
options.Property(x => x.Name)
.HasColumnName("name")
.IsRequired()
.HasMaxLength(100);
options.Property(x => x.DeletedAt)
.HasColumnName("deleted_at")
.HasDefaultValue(null); // Default value null
options.HasQueryFilterSoftDeleteDateTime(); //DateTime?
});
modelBuilder.Entity<People>(options =>
{
options.ToTable("people");
options.HasKey(x => x.Id);
options.Property(x => x.Id)
.HasColumnName("id");
options.Property(x => x.Name)
.HasColumnName("name")
.IsRequired()
.HasMaxLength(100);
options.Property(x => x.DeletedAt)
.HasColumnName("deleted_at")
.HasDefaultValue(false); // Default value false
options.HasQueryFilterSoftDeleteBool(); //Bool
});
modelBuilder.Entity<House>(options =>
{
options.ToTable("house");
options.HasKey(x => x.Id);
options.Property(x => x.Id)
.HasColumnName("id");
options.Property(x => x.Name)
.HasColumnName("name")
.IsRequired().HasMaxLength(100);
options.Property(x => x.DeletedAt)
.HasColumnName("deleted_at")
.HasDefaultValue('N'); // Default value 'N'
options.HasQueryFilterSoftDeleteChar(); //Char
});
}
}