博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DDD Code First 迁移数据实现EF CORE的软删除,值对象迁移配置
阅读量:35193 次
发布时间:2020-01-31

本文共 3346 字,大约阅读时间需要 11 分钟。

感谢Jeffcky大佬的博客:

EntityFramework Core 2.0全局过滤 (HasQueryFilter) https://www.cnblogs.com/CreateMyself/p/8491058.html

什么是值对象

没有唯一的标识,固定不变的,表示一个具体的概念,用来描述一个东西的特征,代表是什么,使用时直接添加或替换,值对象在迁移时,会以字段的形式迁移到数据库中

软删除

定义删除的接口
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
>> GetUserList(SearchUserDto input) { Expression
> where = e => e.IsDisable == false; if (!string.IsNullOrEmpty(input.SearchName)) { where = where.And(e => e.UserName.Contains(input.SearchName)); }
if (!string.IsNullOrEmpty(input.SearchPwd)) { where = where.And(e => e.UserPhone.Contains(input.SearchPwd)); } var userList = await _userRepository.LoadEntityListAsync(where, e => e.UserName, "asc", input.PageIndex, input.Pagesize); var total = await _userRepository.GetEntitiesCountAsync(where); var userDtoList = userList.MapToList
(); HeaderResult
> result = new HeaderResult
> { IsSucceed = true, Result = userDtoList, Total = total }; return result; }
你可能感兴趣的文章
【linux】windows与linux间文件传输
查看>>
【接口平台设计】接口生成
查看>>
【locust】通过ip访问失败
查看>>
【locust】聚合报告无法显示
查看>>
【locust】locust与jmeter性能对比
查看>>
【OS】进程、线程与协程的区别
查看>>
【locust】locust安装与启动
查看>>
【tsung】tsung安装与启动
查看>>
【tsung】tsung分布式运行
查看>>
【tsung】tsung模拟请求
查看>>
【接口平台设计】用例智能推荐
查看>>
【python】pip安装失败问题
查看>>
【Furion】远程运行jmx失败问题
查看>>
【nginx】Docker nginx部署
查看>>
【Flask】before_first_request与processes
查看>>
【mysql】rollup 统计集合
查看>>
【mysql】大数据量数据读取
查看>>
【postgresql】数据聚合之PERCENTILE_CONT
查看>>
【mysql】查询返回int数据为空时处理
查看>>
【SpringBoot】jar包getFile问题
查看>>