Exemple #1
0
 /// <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;
     }
 }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
 /// <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);
     }
 }
Exemple #4
0
        /// <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);
        }