zoukankan      html  css  js  c++  java
  • DDD Code First 迁移数据实现EF CORE的软删除,值对象迁移配置

    感谢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<UserInfo, UserDto>();
    
            HeaderResult<List<UserDto>> result = new HeaderResult<List<UserDto>>
            {
                IsSucceed = true,
                Result = userDtoList,
                Total = total
            };
            return result;
        }
     </pre>
    
  • 相关阅读:
    百度网盘怎么批量删除好友,百度网盘怎么群发消息
    百度网盘不能公开分享了怎么办,怎么批量提取链接?
    百度云/百度网盘私密分享统一设置密码
    pdf.js浏览中文pdf乱码的问题解决
    百度网盘自动更新资源、百度网盘自动更新链接、百度网盘自动更新内容、百度网盘自动更新文件夹、百度网盘怎么自动更新、百度网盘自动更新课件、百度网盘文件自动更新、百度网盘怎么更新文件
    邓西百度网盘批量转存高级版,支持增量更新资料
    邓西百度知道刷赞工具
    spark streaming 3: Receiver 到 submitJobSet
    spark streaming 2: DStream
    spark streaming 1: SparkContex
  • 原文地址:https://www.cnblogs.com/lifeng618/p/10172967.html
Copyright © 2011-2022 走看看