コード例 #1
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 添加实体
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static bool Add <T>(T entity) where T : class
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbEntityEntry entitEntry = _emdc.Entry <T>(entity);
             entitEntry.State = EntityState.Added;
             return(_emdc.SaveChanges() > 0);
         }
     }
     catch (DbEntityValidationException e)
     {
         string exception = "";
         foreach (var eve in e.EntityValidationErrors)
         {
             exception += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
             foreach (var ve in eve.ValidationErrors)
             {
                 exception += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
             }
         }
         exception += "";
         throw;
     }
 }
コード例 #2
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 获取指定条件下的实体集实体数量
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static int Count <T>(Func <T, bool> predicate)
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             return(((IEnumerable <T>)_emdc.Set(typeof(T))).Where(predicate).Count());
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(0);
     }
 }
コード例 #3
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 获取实体列表
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public static List <T> GetList <T>(Func <T, bool> predicate)
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbSet Db_Set = _emdc.Set(typeof(T));
             return(((IEnumerable <T>)Db_Set).Where(predicate).ToList());
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(new List <T>());
     }
 }
コード例 #4
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 获取实体
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public static T Get <T>(Func <T, bool> predicate)
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbSet Db_Set = _emdc.Set(typeof(T));
             return(((IEnumerable <T>)Db_Set).FirstOrDefault(predicate));
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(default(T));
     }
 }
コード例 #5
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 获取实体集实体数量
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static int Count <T>()
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbSet Db_Set = _emdc.Set(typeof(T));
             return(Db_Set.Local.Count);
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(0);
     }
 }
コード例 #6
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 删除实体
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static bool Remove <T>(T entity) where T : class
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbEntityEntry <T> entry = _emdc.Entry <T>(entity);
             entry.State = EntityState.Deleted;
             return(_emdc.SaveChanges() > 0);
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(false);
     }
 }
コード例 #7
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Update <T>(T entity) where T : class
        {
            bool bResult = false;

            try
            {
                using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
                {
                    DbEntityEntry <T> entry = _emdc.Entry <T>(entity);
                    entry.State = EntityState.Modified;
                    return(_emdc.SaveChanges() >= 0);
                }
            }
            catch (Exception ex)
            {
                //
            }
            return(bResult);
        }
コード例 #8
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 删除实体列表
 /// </summary>
 /// <param name="lisT"></param>
 /// <returns></returns>
 public static bool Remove <T>(List <T> lisT)
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbSet Db_Set = _emdc.Set(typeof(T));
             foreach (T temp in Db_Set)
             {
                 Db_Set.Remove(temp);
             }
             return(_emdc.SaveChanges() > 0);
         }
     }
     catch (Exception ex)
     {
         //throw ex;
         return(false);
     }
 }
コード例 #9
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
 /// <summary>
 /// 根据Lamda表达式删除实体
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public static bool Remove <T>(Func <T, bool> predicate)
 {
     try
     {
         using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
         {
             DbSet    Db_Set     = _emdc.Set(typeof(T));
             List <T> entityList = ((IEnumerable <T>)Db_Set).Where(predicate).ToList();
             foreach (T temp in entityList)
             {
                 Db_Set.Remove(temp);
             }
             return(_emdc.SaveChanges() > 0);
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
コード例 #10
0
ファイル: EntityHelper.cs プロジェクト: wspyy/pyyREP
        /// <summary>
        /// 针对修改主键的实体操作(先删除后添加)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool Update <T>(T oldEntity, T newEntity) where T : class
        {
            bool bResult = false;

            try
            {
                using (JinchengDB2Entities _emdc = new JinchengDB2Entities())
                {
                    DbEntityEntry <T> entry = _emdc.Entry <T>(oldEntity);
                    entry.State = EntityState.Deleted;
                    if (_emdc.SaveChanges() > 0)
                    {
                        DbEntityEntry <T> entryNew = _emdc.Entry <T>(newEntity);
                        entryNew.State = EntityState.Added;
                        bResult        = (_emdc.SaveChanges() >= 0);
                    }
                }
            }
            catch (Exception ex)
            {
                //
            }
            return(bResult);
        }