Esempio n. 1
0
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <typeparam name="TEntity"> 要注册的类型 </typeparam>
        /// <param name="entities"> 要注册的对象集合 </param>
        public virtual OperationResult AddNew <TEntity>(IEnumerable <TEntity> entities) where TEntity : EntityBase
        {
            string exMsg = string.Empty;

            try
            {
                using (iCMSDbContext ctx = new iCMSDbContext())
                {
                    EntityState state;
                    //不跟踪变化,提高效率
                    ctx.Configuration.AutoDetectChangesEnabled = false;
                    foreach (TEntity entity in entities)
                    {
                        state = ctx.Entry(entity).State;
                        if (state == EntityState.Detached)
                        {
                            ctx.Entry(entity).State = EntityState.Added;
                        }
                    }
                    ctx.SaveChanges();
                    return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("数据库操作错误:" + ex.Message);
                exMsg = ex.Message;
            }

            return(new OperationResult(EnumOperationResultType.Error, exMsg));
        }
Esempio n. 2
0
        /// <summary>
        ///  add by masu 2016年3月17日09:10:11 批量更新
        /// </summary>
        /// <typeparam name="TEntity"> 要注册的类型 </typeparam>
        /// <param name="entity"> 要注册的对象 </param>
        public virtual OperationResult Update <TEntity>(IEnumerable <TEntity> entities) where TEntity : EntityBase
        {
            string exMsg = string.Empty;

            try
            {
                using (iCMSDbContext ctx = new iCMSDbContext())
                {
                    // EntityState state;
                    //不跟踪变化,提高效率
                    ctx.Configuration.AutoDetectChangesEnabled = false;
                    foreach (TEntity entity in entities)
                    {
                        if (ctx.Entry(entity).State == EntityState.Detached)
                        {
                            ctx.Entry(entity).State = EntityState.Modified;
                        }
                    }

                    ctx.SaveChanges();

                    ////批量
                    //foreach (TEntity entity in entities)
                    //{
                    //    if (ctx.Entry(entity).State == EntityState.Detached)
                    //    {
                    //        try
                    //        {
                    //            #region 云推送添加
                    //            BaseCloudService cloud = BaseCloudService.GetInstance();
                    //            cloud.SyncInfo(entity, EnumCloudDataOperation.Update);
                    //            #endregion
                    //        }
                    //        catch (Exception ex)
                    //        {
                    //            LogHelper.WriteLog(ex);
                    //        }
                    //        finally
                    //        {

                    //        }

                    //        //Thread thread = new Thread(YunyiThreadHelper.YunyiUpdate);
                    //        //thread.Start(entity);
                    //    }
                    //}

                    return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("数据库操作错误:" + ex.Message);
                exMsg = ex.Message;
            }
            return(new OperationResult(EnumOperationResultType.Error, exMsg));
        }
Esempio n. 3
0
        /// <summary>
        ///  更新
        /// </summary>
        /// <typeparam name="TEntity"> 要注册的类型 </typeparam>
        /// <param name="entity"> 要注册的对象 </param>
        public virtual OperationResult Update <TEntity>(TEntity entity) where TEntity : EntityBase
        {
            string exMsg = string.Empty;

            try
            {
                using (iCMSDbContext ctx = new iCMSDbContext())
                {
                    if (ctx.Entry(entity).State == EntityState.Detached)
                    {
                        ctx.Set <TEntity>().Attach(entity);
                    }

                    ctx.Entry(entity).State = EntityState.Modified;
                    ctx.SaveChanges();

                    //try
                    //{
                    //    #region 云推送添加
                    //    BaseCloudService cloud = BaseCloudService.GetInstance();
                    //    cloud.SyncInfo(entity, EnumCloudDataOperation.Update);
                    //    #endregion
                    //}
                    //catch (Exception ex)
                    //{
                    //    LogHelper.WriteLog(ex);
                    //}
                    //finally
                    //{

                    //}

                    #region 云平台修改 Multi Thread

                    //Thread thread = new Thread(YunyiThreadHelper.YunyiUpdate);
                    //thread.Start(entity);

                    #endregion

                    return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
                }
            }
            catch (Exception ex)
            {
                exMsg = ex.Message;

                LogHelper.WriteLog("数据库操作错误:" + ex.Message);
            }
            return(new OperationResult(EnumOperationResultType.Error, exMsg));
        }
Esempio n. 4
0
        /// <summary>
        ///     删除指定编号的记录
        /// </summary>
        /// <param name="id"> 实体记录编号 </param>
        /// <returns> 操作影响的行数 </returns>
        public virtual OperationResult Delete <TEntity>(int id) where TEntity : EntityBase
        {
            try
            {
                using (iCMSDbContext ctx = new iCMSDbContext())
                {
                    TEntity entity = Set <TEntity>(ctx).Find(id);

                    ctx.Entry(entity).State = EntityState.Deleted;
                    ctx.SaveChanges();
                    // return Delete<TEntity>(entities);
                    return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("数据库操作错误:" + ex.Message);
                return(new OperationResult(EnumOperationResultType.Error, ex.Message));
            }
        }
Esempio n. 5
0
 /// <summary>
 ///     删除所有符合特定表达式的数据
 /// </summary>
 /// <param name="predicate"> 查询条件谓语表达式 </param>
 /// <returns> 操作影响的行数 </returns>
 public virtual OperationResult Delete(Expression <Func <TEntity, bool> > predicate)
 {
     try
     {
         using (iCMSDbContext ctx = new iCMSDbContext())
         {
             List <TEntity> entities = Set <TEntity>(ctx).Where(predicate).ToList();
             foreach (TEntity entity in entities)
             {
                 ctx.Entry(entity).State = EntityState.Deleted;
             }
             ctx.SaveChanges();
             // return Delete<TEntity>(entities);
             return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
         }
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog("数据库操作错误:" + ex.Message);
         return(new OperationResult(EnumOperationResultType.Error, ex.Message));
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 事务操作
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public OperationResult TranMethod(Dictionary <EntityBase, EntityState> models)
        {
            string exMsg = string.Empty;

            try
            {
                using (iCMSDbContext ctx = new iCMSDbContext())
                {
                    foreach (var model in models)
                    {
                        ctx.Entry(model.Key).State = model.Value;
                    }

                    ctx.SaveChanges();

                    //#region 云平台推送

                    //try
                    //{
                    //    Thread thread = null;
                    //    BaseCloudService cloud = BaseCloudService.GetInstance();
                    //    foreach (var model in models)
                    //    {
                    //        switch (model.Value)
                    //        {
                    //            case EntityState.Added:
                    //                #region 云推送添加
                    //                cloud.SyncInfo(model.Key, EnumCloudDataOperation.Add);
                    //                #endregion
                    //                break;
                    //            case EntityState.Deleted:
                    //                #region 云推送删除
                    //                cloud.SyncInfo(model.Key, EnumCloudDataOperation.Delete);
                    //                #endregion
                    //                break;
                    //            case EntityState.Modified:
                    //                #region 云推送删除
                    //                cloud.SyncInfo(model.Key, EnumCloudDataOperation.Update);
                    //                #endregion
                    //                break;
                    //        }
                    //    }

                    //}
                    //catch (Exception ex)
                    //{
                    //    LogHelper.WriteLog(ex);
                    //}
                    //finally
                    //{

                    //}

                    //#endregion
                    return(new OperationResult(EnumOperationResultType.Success, "操作成功!"));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("数据库操作错误:" + ex.Message);
                exMsg = ex.Message;
            }
            finally
            {
                // Context.Configuration.AutoDetectChangesEnabled = true;
            }
            return(new OperationResult(EnumOperationResultType.Error, exMsg));
        }