Example #1
0
        /// <summary>
        /// 获取插入或更新所参照的实体。
        /// </summary>
        /// <param name="instances"></param>
        /// <param name="batchOpt"></param>
        /// <returns></returns>
        internal static List <string> GetModifiedProperties(Expression instances, BatchOperateOptions batchOpt)
        {
            if (!(instances is ConstantExpression consExp) || !(consExp.Value is IEnumerable entities))
            {
                return(null);
            }

            var  maxProperties = new List <string>();
            Type entityType    = null;
            IEnumerable <IProperty> properties = null;

            //循环列表查找属性修改最多的那个实体作为参照
            foreach (IEntity entity in entities)
            {
                if (entityType == null)
                {
                    entityType = entity.GetType();
                    properties = PropertyUnity.GetPersistentProperties(entityType)
                                 .Where(p => !p.Info.IsPrimaryKey ||
                                        (p.Info.IsPrimaryKey && p.Info.GenerateType == IdentityGenerateType.None))
                                 .ToList();
                }

                //判断实体类型有是不是编译的代理类型,如果不是,取非null的属性,否则使用IsModified判断
                var isNotCompiled = entityType.IsNotCompiled();
                var modified      = isNotCompiled ?
                                    properties.Where(p => !PropertyValue.IsEmpty(entity.GetValue(p))).Select(p => p.Name).ToArray() :
                                    entity.GetModifiedProperties();

                modified.Where(s => !maxProperties.Contains(s)).ForEach(s => maxProperties.Add(s));

                if (batchOpt == null || batchOpt.CheckModifiedKinds == BatchCheckModifiedKinds.First)
                {
                    break;
                }
            }

            return(maxProperties);
        }
 /// <summary>
 /// 异步的,对实体集合进行批量操作。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="instances"></param>
 /// <param name="fnOperation"></param>
 /// <param name="cancellationToken">取消操作的通知。</param>
 /// <returns>影响的实体数。</returns>
 public async Task <int> BatchAsync(IEnumerable <TEntity> instances, Expression <Func <IRepository <TEntity>, TEntity, int> > fnOperation, BatchOperateOptions batchOpt, CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// 对实体集合进行批量操作。
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="instances"></param>
 /// <param name="fnOperation"></param>
 /// <returns>影响的实体数。</returns>
 public int Batch(IEnumerable <TEntity> instances, Expression <Func <IRepository <TEntity>, TEntity, int> > fnOperation, BatchOperateOptions batchOpt)
 {
     throw new NotImplementedException();
 }