阅读目录
public interface ISoftDelete { bool IsDeleted { get; set; } } 新增AI编程课程,引领技术教育新趋势
阅读目录
public interface ISoftDelete { bool IsDeleted { get; set; } } 创建模型实现ISoftDelete接口
public class UserInfo : IAggregationRoot, ISoftDelete { public Guid Id { get; set; } public string UserName { get; private set; } public string UserPassword { get; private set; } public string UserPhone { get; private set; } public Address Address { get; private set; } public bool IsDeleted { get; set; } } [Owned] public class Address:IValueObject { public string Province { get;private set; } public string City { get; private set; } public string County { get; private set; } public string AddressDetails { get; private set; } } Lamda的扩展以及Code First 迁移配置
protected override void OnModelCreating(ModelBuilder modelBuilder) { //设置软删除 foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var parameter = Expression.Parameter(entityType.ClrType); //查询类上面是否有Owned(值对象)的特性 var ownedModelType = parameter.Type; var ownedAttribute = Attribute.GetCustomAttribute(ownedModelType, typeof(OwnedAttribute)); if (ownedAttribute == null) { var propertyMethodInfo = typeof(EF).GetMethod("Property").MakeGenericMethod(typeof(bool)); var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDeleted")); BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false)); var lambda = Expression.Lambda(compareExpression, parameter); modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda); } } } 在这里需要过滤掉值对象的类,在值对象的类上面声明一个特性,通过该特性过滤掉该值对象, 如果该类是值对象就直接跳过,不过滤值对象EF CORE会给值对象附加一个IsDeleted的字段,EF CORE执行中会报错,提示找不到该字段
Owned是EF CORE 配置值对象的特性,可以去自定义特性,在每一个值对象上面声明,在OnModelCreating 过滤掉包含这个特性的类
最终实现的代码:
public async Task