I want to set a condition on all entites to hide the ones that are deleted.
I could not find aa command in the Fluent Api to achieve this.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BankAccountCurrencyType>().HasQueryFilter(x => x.IsDeleted == false || x.IsDeleted ==null );
}
2
For achieving this, you should first define a baseClass for all entities like this. (Nullable IsDeleted, depends on your scenario and business):
/// <summary>
/// Base class for entities in the application.
/// </summary>
public abstract class BaseEntity
{
// Properties
public virtual bool? IsDeleted { get; set; }
}
Then You should write a configuration for this base class like bellow to apply Global query filter.
/// <summary>
/// Represents the abstract entity configuration for the <see cref="BaseEntity"/>> entity.
/// </summary>
public class BaseEntityConfiguration<TEntityType> : IEntityTypeConfiguration<TEntityType> where TEntityType : BaseEntity
{
public virtual void Configure(EntityTypeBuilder<TEntityType> builder)
{
// Retrieve all active and un deleted objects
builder.HasQueryFilter(p => p.IsDeleted != null && p.IsDeleted != false);
}
}
Then for configuring other entities you should follow like this. Assume that you have a billing entity, then configuration would be like this:
/// <summary>
/// Represents the entity configuration for the <see cref="Billing"/>> entity.
/// </summary>
public class BillingConfiguration : BaseEntityConfiguration<Billing>
{
public override void Configure(EntityTypeBuilder<Billing> builder)
{
base.Configure(builder);
// Other configuration specially for Billing Entity
}
}
Hope this help you. any question, comment plz.
1