Exemplo n.º 1
0
        //public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
        //public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void Update(TEntity entity)
        {
            try
            {
                var entry = EF.Entry(entity);
                EF.Set <TEntity>().Attach(entity);
                entry.State = EntityState.Modified;
            }
            catch (OptimisticConcurrencyException ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
 public int Insert <TEntity1>(TEntity1 entity) where TEntity1 : class
 {
     EF.Set <TEntity1>().Add(entity);
     return(EF.SaveChanges());
 }
Exemplo n.º 3
0
        //public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
        //public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(TEntity entities)
        {
            if (EF == null)
            {
                throw new ArgumentNullException("dbContext");
            }
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }


            DbSet <TEntity> dbSet = EF.Set <TEntity>();

            try
            {
                System.Data.Entity.Infrastructure.DbEntityEntry <TEntity> entry = EF.Entry(entities);
                if (entry.State == EntityState.Detached)
                {
                    dbSet.Attach(entities);
                    entry.State = EntityState.Modified;
                }
            }
            catch (InvalidOperationException)
            {
                TEntity oldEntity = dbSet.Find(entities.ID);
                EF.Entry(oldEntity).CurrentValues.SetValues(entities);
            }
            return(SaveChanges());
        }