Esempio n. 1
0
        protected int UpdateMany(IEnumerable items, ISchema schema, IDataUpdateOptions options = null)
        {
            //确认是否可以执行该操作
            this.EnsureUpdate(options);

            if (items == null)
            {
                return(0);
            }

            //构建数据操作的选项对象
            if (options == null)
            {
                options = new DataUpdateOptions();
            }

            //进行授权验证
            this.Authorize(DataServiceMethod.UpdateMany(), options);

            //将当前更新数据集合对象转换成数据字典集合
            var dictionares = DataDictionary.GetDictionaries <TModel>(items);

            //修正数据模式对象
            schema = this.Schematic(schema, Common.TypeExtension.GetElementType(items.GetType()));

            foreach (var dictionary in dictionares)
            {
                //验证待更新的数据
                this.OnValidate(DataServiceMethod.UpdateMany(), schema, dictionary, options);
            }

            return(this.OnUpdateMany(dictionares, schema, options));
        }
Esempio n. 2
0
        protected int Update(object data, ICondition criteria, ISchema schema, IDataUpdateOptions options = null)
        {
            //确认是否可以执行该操作
            this.EnsureUpdate(options);

            if (data == null)
            {
                return(0);
            }

            //构建数据操作的选项对象
            if (options == null)
            {
                options = new DataUpdateOptions();
            }

            //进行授权验证
            this.Authorize(DataServiceMethod.Update(), options);

            //将当前更新数据对象转换成数据字典
            var dictionary = DataDictionary.GetDictionary <TModel>(data);

            //如果指定了更新条件,则尝试将条件中的主键值同步设置到数据字典中
            if (criteria != null)
            {
                //获取当前数据服务的实体主键集
                var keys = this.DataAccess.Metadata.Entities.Get(this.Name).Key;

                if (keys != null && keys.Length > 0)
                {
                    foreach (var key in keys)
                    {
                        criteria.Match(key.Name, c => dictionary.TrySetValue(c.Name, c.Value));
                    }
                }
            }

            //修整过滤条件
            criteria = this.OnValidate(DataServiceMethod.Update(), criteria ?? this.GetUpdateKey(dictionary), options.Filter, options);

            //修正数据模式对象
            schema = this.Schematic(schema, data.GetType());

            //验证待更新的数据
            this.OnValidate(DataServiceMethod.Update(), schema, dictionary, options);

            //如果缺少必须的更新条件则抛出异常
            if (criteria == null)
            {
                //再次从数据中获取主键条件
                criteria = this.GetUpdateKey(dictionary);

                if (criteria == null)
                {
                    throw new DataOperationException($"The update operation of the specified ‘{this.Name}’ entity missing required conditions.");
                }
            }

            //执行更新操作
            return(this.OnUpdate(dictionary, criteria, schema, options));
        }