/// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns></returns>
        public bool Delete(Guid uuid)
        {
            TreeviewContext context    = new TreeviewContext();
            var             dataSource = context.TreeNodeSource;
            var             oldObj     = dataSource.FirstOrDefault(o => o.UUID == uuid);

            if (oldObj == null)
            {
                return(true);
            }
            List <Guid> needDeleteUUIDs = new List <Guid>();

            if (HasChildren(uuid, ref context))
            {
                GetDeleteChildren(uuid, ref context, ref needDeleteUUIDs);
                if (needDeleteUUIDs.Count > 0)
                {
                    foreach (var id in needDeleteUUIDs)
                    {
                        var tempObj = dataSource.FirstOrDefault(o => o.UUID == id);
                        if (tempObj == null)
                        {
                            continue;
                        }
                        dataSource.Remove(tempObj);
                    }
                }
            }
            dataSource.Remove(oldObj);
            if (context.SaveChanges() > 0)
            {
                return(true);
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 通过一个新的对象来更新旧对象,
        /// 具体做法是将新对象中的属性的
        /// 值全部复制到旧对象中更新
        /// </summary>
        /// <param name="newobj"></param>
        /// <returns></returns>
        public static TEntity Update(TEntity newobj)
        {
            TreeviewContext context = new TreeviewContext();
            var             oldObj  = context.GetDatasource <TEntity>().FirstOrDefault(o => o.UUID == newobj.UUID);

            if (oldObj == null)
            {
                throw new Exception("未找到需要更新的元素");
            }
            Common.UpdateObj(ref oldObj, newobj);
            if (context.SaveChanges() > 0)
            {
                return(newobj);
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// 删除一个元素,如果元素不存在,则直接返回true
        /// </summary>
        /// <param name="uuid"></param>
        /// <returns>
        /// 如果元素不存在,返回true
        /// 删除成功返回true
        /// 如果删除失败,则返回false
        /// </returns>
        public static bool Delete(Guid uuid)
        {
            TreeviewContext context    = new TreeviewContext();
            var             dataSource = context.GetDatasource <TEntity>();
            var             oldObj     = dataSource.FirstOrDefault(o => o.UUID == uuid);

            if (oldObj == null)
            {
                return(true);
            }
            dataSource.Remove(oldObj);
            if (context.SaveChanges() > 0)
            {
                return(true);
            }
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// 添加一个新的对象,并且该函数会给对象自动赋予UUID,
        /// 如果外部已经有生成一个UUID,执行该函数后UUID会
        /// 被重写,如果数据插入成功,将返回该对象,如果出错,那么
        /// 则返回一个空对象.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static TEntity Add(TEntity e)
        {
            if (Exists(e))
            {
                throw new EntityExistsException("Entity has exists.");
            }
            if (e.UUID == Guid.Empty)
            {
                e.UUID = Guid.NewGuid();
            }
            TreeviewContext context = new TreeviewContext();

            context.GetDatasource <TEntity>().Add(e);
            int flag = context.SaveChanges();

            if (flag > 0)
            {
                return(e);
            }
            return(null);
        }