示例#1
0
 public void Delete(List <T> entitys)
 {
     dbWrite = DBContextFactory.GetService();
     foreach (var t in entitys)
     {
         dbWrite.Set <T>().Attach(t);
         dbWrite.Set <T>().Remove(t);
     }
 }
示例#2
0
        public void Delete(Expression <Func <T, bool> > whereLambda)
        {
            dbWrite = DBContextFactory.GetService();
            var entitys = dbWrite.Set <T>().Where(whereLambda).ToList();

            foreach (var t in entitys)
            {
                dbWrite.Set <T>().Attach(t);
                dbWrite.Set <T>().Remove(t);
            }
        }
示例#3
0
        private const int TrySaveCount          = 2; //服务端重试2次

        /// <summary>
        /// 提交保存更改
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            lock (LockSave)
            {
                int iSave  = 1; //最后一次写入失败后抛出异常
                int result = 0;

                #region 重复写,最后一次抛出异常

                while (iSave < TrySaveCount)
                {
                    try
                    {
                        result = dbWrite.SaveChanges(); //保存提交
                        return(result > 0);             //
                    }
                    catch (Exception)                   //写入失败
                    {
                        Console.WriteLine($"!!!!!!!!!!! 第【{iSave}】次写入数据库失败 !!!!!!!!!!!!");
                        iSave++;
                        Thread.Sleep(100); //暂停100ms
                    }
                }

                #endregion

                #region 最后一次写入,失败后抛出异常

                try
                {
                    result = dbWrite.SaveChanges(); //保存提交
                    return(result > 0);             //
                }
                catch (Exception ex)                //写入失败
                {
                    Console.WriteLine($"!!!!!!!!!!! 多次写入数据库失败,共尝试【{TrySaveCount}】次,请检查 !!!!!!!!!!!!");
                    dbWrite = DBContextFactory.GetService(); //保存失败,重新获取一个EF缓存
                    throw new Exception(
                              $"写入数据库失败,\r\n错误为:Message:{ex.Message}\r\nStackTrace:{ex.StackTrace}\r\nInnerException:{ex.InnerException?.Message}");
                }

                #endregion
            }
        }
示例#4
0
 static SqlDAL()                    //静态gou
 {
     DBContextFactory.GetService(); //
 }
示例#5
0
 public void Delete(T t)
 {
     dbWrite = DBContextFactory.GetService();
     dbWrite.Set <T>().Attach(t);
     dbWrite.Set <T>().Remove(t);
 }