I've created an interface to try to do a soft delete, mixing shadow properties and query filters. But it's not working.
public interface IDeletableEntity {}
And then in my model builder
builder.Model.GetEntityTypes()
.Where(entityType => typeof(IDeletableEntity).IsAssignableFrom(entityType.ClrType))
.ToList()
.ForEach(entityType =>
{
builder.Entity(entityType.ClrType).Property<Boolean>("IsDeleted");
builder.Entity(entityType.ClrType).HasQueryFilter(e => EF.Property<Boolean>(e, "IsDeleted") == false);
});
But the line with the query filter doesn't compile. The error I got is "cannot convert lambda expression to type 'lambda expression' because it is not a delegate type"
If I do this it's working.
builder.Entity<MyEntity>().HasQueryFilter(m => EF.Property<Boolean>(m, "IsDeleted") == false);
there are any way to do this? It's in order to have an Interface with IDeletableEntity and not have to do it, in every Entity that I want to use a soft delete Entity
Many thanks in advance,
I've found a simple solution for my answer ;-). Thanks anyway Ivan Stoev
The interface is:
public interface IDeletableEntity
{
bool IsDeleted { get; }
}
And in your model Builder configuration:
builder.Model.GetEntityTypes()
.Where(entityType => typeof(IDeletableEntity).IsAssignableFrom(entityType.ClrType))
.ToList()
.ForEach(entityType =>
{
builder.Entity(entityType.ClrType)
.HasQueryFilter(ConvertFilterExpression<IDeletableEntity>(e => !e.IsDeleted, entityType.ClrType));
});
You need to convertfilterExpression
private static LambdaExpression ConvertFilterExpression<TInterface>(
Expression<Func<TInterface, bool>> filterExpression,
Type entityType)
{
var newParam = Expression.Parameter(entityType);
var newBody = ReplacingExpressionVisitor.Replace(filterExpression.Parameters.Single(), newParam, filterExpression.Body);
return Expression.Lambda(newBody, newParam);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With