Example #1
0
        /// <summary>
        /// 清除子对象集合
        /// </summary>
        /// <param name="oldEntity">The old entity.</param>
        /// <param name="entityInfo">The entity information.</param>
        protected virtual void MakeSavedCore(Entity oldEntity, EntityMeta entityInfo)
        {
            foreach (var item in entityInfo.ChildrenProperties)
            {
                var mp = item.ManagedProperty as IListProperty;

                //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值
                if (!oldEntity.FieldExists(mp)) continue;

                var children = oldEntity.GetProperty(mp) as EntityList;
                if (children == null) continue;

                //清除已删除数据
                children.CastTo<EntityList>().DeletedList.Clear();

                //所有子对象,都标记为已保存
                for (int i = children.Count - 1; i >= 0; i--)
                {
                    var child = children[i] as Entity;
                    if (child.IsDirty || child.IsNew) MakeSaved(child);
                }
            }

            oldEntity.MarkSaved();
        }
Example #2
0
        /// <summary>
        /// 删除不必要的对象,只留下需要保存的“脏”数据
        /// </summary>
        /// <param name="diffEntity">The difference entity.</param>
        /// <param name="entityInfo">The entity information.</param>
        protected virtual void ClearDataCore(Entity diffEntity, EntityMeta entityInfo)
        {
            foreach (var item in entityInfo.ChildrenProperties)
            {
                var mp = item.ManagedProperty;

                //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值
                if (!diffEntity.FieldExists(mp)) continue;
                var children = diffEntity.GetProperty(mp) as EntityList;
                if (children == null) continue;

                for (int i = children.Count - 1; i >= 0; i--)
                {
                    var child = children[i];
                    if (!child.IsDirty)
                    {
                        children.Remove(child);
                        children.DeletedList.Remove(child);
                    }
                    else
                    {
                        this.ClearData(child);
                    }
                }
            }
        }
Example #3
0
        internal int Update(IDbAccesser dba, Entity item)
        {
            EnsureMappingTable();

            var parameters = new List<object>(10);
            string updateSql = null;

            //是否有需要更新的 lob 字段。
            bool hasUpdatedLOBColumns = false;
            List<RdbColumn> lobColumns = null;

            if (_hasLOB)
            {
                lobColumns = new List<RdbColumn>();

                for (int i = 0, c = _columns.Count; i < c; i++)
                {
                    var column = _columns[i];
                    //如果一个 lob 属性的值存在,则表示需要更新。
                    //(可能被设置了,也可能只是简单地读取了一下,没有变更值。这时也简单地处理。)
                    if (column.IsLOB && item.FieldExists(column.Info.Property))
                    {
                        lobColumns.Add(column);
                        hasUpdatedLOBColumns = true;
                    }
                }
            }

            if (!hasUpdatedLOBColumns)
            {
                //如果没有 LOB,则直接缓存上更新语句。
                if (_updateSQL == null) { _updateSQL = this.GenerateUpdateSQL(null); }
                updateSql = _updateSQL;
            }
            else
            {
                updateSql = this.GenerateUpdateSQL(lobColumns);
            }

            //更新所有非 lob 的字段
            for (int i = 0, c = _columns.Count; i < c; i++)
            {
                var column = _columns[i];
                if (!column.Info.IsPrimaryKey && !column.IsLOB)
                {
                    var value = column.ReadParameterValue(item);
                    parameters.Add(value);
                }
            }

            //更新需要更新的 lob 字段
            if (hasUpdatedLOBColumns)
            {
                for (int i = 0, c = lobColumns.Count; i < c; i++)
                {
                    var column = lobColumns[i];
                    if (!column.Info.IsPrimaryKey)
                    {
                        var value = column.ReadParameterValue(item);
                        parameters.Add(value);
                    }
                }
            }

            //Id 最后加入
            parameters.Add(item.Id);

            int res = dba.ExecuteText(updateSql, parameters.ToArray());
            return res;
        }