Exemplo n.º 1
0
        public virtual int Update(T entity, params Expression <Func <T, object> >[] updatedProperties)
        {
            using (var dbContext = new DcContext())
            {
                //entity=dbContext.Set<T>().Where(c => c.Id == 1).FirstOrDefault();
                var dbEntityEntry = dbContext.Entry(entity);
                if (dbEntityEntry.Entity == null || dbEntityEntry.State == EntityState.Detached)
                {
                    var dbEntityAttach = dbContext.Set <T>().Attach(entity);
                    dbEntityEntry = dbContext.Entry(entity);
                }

                if (updatedProperties.Any())
                {
                    foreach (var property in updatedProperties)
                    {
                        dbEntityEntry.Property(property).IsModified = true;
                    }
                }
                else
                {
                    foreach (var property in dbEntityEntry.OriginalValues.PropertyNames)
                    {
                        var original = dbEntityEntry.OriginalValues.GetValue <object>(property);
                        var current  = dbEntityEntry.CurrentValues.GetValue <object>(property);
                        if (original != null && !original.Equals(current))
                        {
                            dbEntityEntry.Property(property).IsModified = true;
                        }
                    }
                }
                return(dbContext.SaveChanges());
            }
        }
Exemplo n.º 2
0
        //public virtual int Add(IEnumerable<T> entities, bool isUseBulkInsert = false)
        //{
        //    using (var dbContext = new DcContext())
        //    {
        //        if (isUseBulkInsert)
        //        {
        //            dbContext.BulkInsert(entities);
        //        }
        //        else
        //        {
        //            foreach (var entity in entities)
        //            {
        //                dbContext.Set<T>().Add(entity);
        //            }
        //        }
        //        return dbContext.SaveChanges();
        //    }
        //}

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual int Update(T entity)
        {
            using (var dbContext = new DcContext())
            {
                if (!dbContext.Set <T>().Local.Contains(entity))
                {
                    dbContext.Set <T>().Attach(entity);
                }
                dbContext.Entry(entity).State = EntityState.Modified;
                return(dbContext.SaveChanges());
            }
        }
Exemplo n.º 3
0
 public virtual int Delete(long id)
 {
     using (var dbContext = new DcContext())
     {
         var dbEntityEntry = dbContext.Entry(new T()
         {
             Id        = id,
             IsDeleted = 1
         });
         dbEntityEntry.Property("Id").IsModified = true;
         return(dbContext.SaveChanges());
     }
 }
Exemplo n.º 4
0
 public virtual int Update(IEnumerable <T> entitys)
 {
     using (var dbContext = new DcContext())
     {
         foreach (var entity in entitys)
         {
             if (!dbContext.Set <T>().Local.Contains(entity))
             {
                 dbContext.Set <T>().Attach(entity);
             }
             dbContext.Entry(entity).State = EntityState.Modified;
         }
         return(dbContext.SaveChanges());
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 批量删除实体 by some where
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public virtual int Deletes(Expression <Func <T, bool> > where)
 {
     using (var dbContext = new DcContext())
     {
         foreach (var entity in dbContext.Set <T>().Where(where))
         {
             if (entity is ISoftDelete)
             {
                 (entity as ISoftDelete).IsDeleted = true;
                 dbContext.Entry <T>(entity).State = EntityState.Modified;
             }
             else
             {
                 dbContext.Set <T>().Remove(entity);
             }
         }
         return(dbContext.SaveChanges());
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 删除实体
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public virtual int Delete(T entity)
 {
     using (var dbContext = new DcContext())
     {
         if (!dbContext.Set <T>().Local.Contains(entity))
         {
             dbContext.Set <T>().Attach(entity);
         }
         if (entity is ISoftDelete)
         {
             (entity as ISoftDelete).IsDeleted = true;
             dbContext.Entry <T>(entity).State = EntityState.Modified;
         }
         else
         {
             if (!dbContext.Set <T>().Local.Contains(entity))
             {
                 dbContext.Set <T>().Attach(entity);
             }
             dbContext.Set <T>().Remove(entity);
         }
         return(dbContext.SaveChanges());
     }
 }