ef core SoftDelete Multi-tenancy 软删除、多租户实现 Global Query Filters

泛型:

// 1. Add the IsDeleted property
entityType.GetOrAddProperty("IsDeleted", typeof(bool));

// 2. Create the query filter

var parameter = Expression.Parameter(entityType.ClrType);

// EF.Property<bool>(post, "IsDeleted")
var propertyMethodInfo = typeof(EF).GetMethod("Property").MakeGenericMethod(typeof(bool));
var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDeleted"));

// EF.Property<bool>(post, "IsDeleted") == false
BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false));

// post => EF.Property<bool>(post, "IsDeleted") == false
var lambda = Expression.Lambda(compareExpression, parameter);

builder.Entity(entityType.ClrType).HasQueryFilter(lambda);

直接:

 modelBuilder.Entity<Post>().HasQueryFilter(p => !p.IsDeleted);

https://spin.atomicobject.com/2019/01/29/entity-framework-core-soft-delete/

https://www.meziantou.net/entity-framework-core-soft-delete-using-query-filters.htm

https://docs.microsoft.com/en-us/ef/core/querying/filters

猜你喜欢

转载自www.cnblogs.com/wswind/p/11943517.html