Exemplo n.º 1
0
        public EduAnswerEntity Get(string id)
        {
            var entity = eduAnswerEntities.Find(id);

            _context.Entry(entity).State = EntityState.Detached;
            return(entity);
        }
Exemplo n.º 2
0
 public void Adicionar(T entidad)
 {
     if (_context.Entry(entidad).State != EntityState.Detached)
     {
         _context.Entry(entidad).State = EntityState.Added;
     }
     else
     {
         _context.Set <T>().Add(entidad);
     }
 }
        public int Update <T>(T t) where T : class
        {
            PropertyInfo updateDate = typeof(T).GetProperty("UpdateDate");

            if (updateDate != null)
            {
                updateDate.SetValue(t, DateTime.Now);//默认修改时间
            }
            //typeof(T).GetProperty("UpdateID").SetValue(t, _cacheManager.Get<User>(CacheKeyEnum.CurrentUserKey)?.ID ?? 0);
            _dbContext.Entry <T>(t).State = EntityState.Modified;
            return(SaveChanges());
        }
Exemplo n.º 4
0
 public static TEntity Attach <TEntity>(this System.Data.Entity.DbContext dbContext, TEntity entity, bool isNew) where TEntity : class
 {
     if (isNew)
     {
         entity = dbContext.Set <TEntity>().Add(entity);
         dbContext.Entry(entity).State = EntityState.Added;
     }
     else
     {
         entity = dbContext.Set <TEntity>().Attach(entity);
         dbContext.Entry(entity).State = EntityState.Modified;
     }
     return(entity);
 }
Exemplo n.º 5
0
 public void Update(TEntity entity)
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     Dbset.Attach(entity);
     m_dataContext.Entry(entity).State = EntityState.Modified;
 }
Exemplo n.º 6
0
        private void ChangeState(T entity, EntityState state)
        {
            var entry = _context.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                _context.Set <T>().Attach(entity);
            }

            entry.State = state;
        }
Exemplo n.º 7
0
 public void Update(T entity)
 {
     try
     {
         if (entity == null)
         {
             throw new ArgumentNullException("entity");
         }
         Dbset.Attach(entity);
         _dataContext.Entry(entity).State = EntityState.Modified;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// 获取给定实体的 <see cref="DbEntityEntry"/> 对象,以便提供对与该实体有关的信息的访问以及对实体执行操作的功能。
        /// <para>参数 <paramref name="updateValues"/> 指示在找到该实体对象后是否根据传入的 <paramref name="entity"/> 实体值更新找到的对象。</para>
        /// </summary>
        /// <param name="_this"></param>
        /// <param name="entity"></param>
        /// <param name="updateValues"></param>
        /// <returns></returns>
        public static DbEntityEntry FindEntry(this System.Data.Entity.DbContext _this, object entity, bool updateValues)
        {
            Check.NotNull(entity);
            DbEntityEntry entry = _this.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                Type entityType = entity.GetType();
                var  entries    = (from item in _this.ChangeTracker.Entries()
                                   where item != null && item.Entity != null
                                   let type = item.Entity.GetType()
                                              where type == entityType || type.IsSubclassOf(entityType) || entityType.IsSubclassOf(type)
                                              select item).ToArray();

                if (entries.Length > 0)
                {
                    EntityTable table = GetEntityTable(_this, entityType);
                    if (table != null)
                    {
                        var properties = table.ModelType.KeyProperties;
                        var temp       = entries.FirstOrDefault(
                            item =>
                        {
                            return(properties.All(
                                       property =>
                            {
                                return object.Equals(item.Property(property.Name).CurrentValue, entry.Property(property.Name).CurrentValue);
                            }));
                        });

                        if (temp != null)
                        {
                            if (updateValues)
                            {
                                temp.CurrentValues.SetValues(entity);
                            }

                            entry = temp;
                        }
                    }
                }
            }
            return(entry);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 获取给定实体的 <see cref="DbEntityEntry&lt;TEntity&gt;"/> 对象,以便提供对与该实体有关的信息的访问以及对实体执行操作的功能。
        /// <para>参数 <paramref name="updateValues"/> 指示在找到该实体对象后是否根据传入的 <paramref name="entity"/> 实体值更新找到的对象。</para>
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="_this"></param>
        /// <param name="entity"></param>
        /// <param name="updateValues"></param>
        /// <returns></returns>
        public static DbEntityEntry <TEntity> FindEntry <TEntity>(this System.Data.Entity.DbContext _this, TEntity entity, bool updateValues)
            where TEntity : class, new()
        {
            Check.NotNull(entity);
            DbEntityEntry <TEntity> entry = _this.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                var entries = (from item in _this.ChangeTracker.Entries <TEntity>()
                               where item != null && item.Entity != null
                               select item).ToArray();

                if (entries.Length > 0)
                {
                    EntityTable table = GetEntityTable <TEntity>(_this);
                    if (table != null)
                    {
                        var properties = table.ModelType.KeyProperties;
                        var temp       = entries.FirstOrDefault(
                            item =>
                        {
                            return(properties.All(
                                       property =>
                            {
                                return object.Equals(item.Property(property.Name).CurrentValue, entry.Property(property.Name).CurrentValue);
                            }));
                        });

                        if (temp != null && updateValues)
                        {
                            if (updateValues)
                            {
                                temp.CurrentValues.SetValues(entity);
                            }

                            entry = temp;
                        }
                    }
                }
            }
            return(entry);
        }
Exemplo n.º 10
0
 public void Detach(TEntity entity)
 {
     context.Entry <TEntity>(entity).State = EntityState.Detached;
 }
Exemplo n.º 11
0
 public static void ReloadNavigationProperty <TEntity, TElement>([NotNull] this SystemDbContext thisValue, TEntity entity, Expression <Func <TEntity, ICollection <TElement> > > navigationProperty)
     where TEntity : class
     where TElement : class
 {
     thisValue.Entry(entity).Collection(navigationProperty).Query();
 }
Exemplo n.º 12
0
 public static Task ReloadAsync <TEntity>([NotNull] this SystemDbContext thisValue, TEntity entity, CancellationToken cancellationToken)
     where TEntity : class
 {
     return(thisValue.Entry(entity).ReloadAsync(cancellationToken));
 }
Exemplo n.º 13
0
 public void Delete(FileInfoEntity entity)
 {
     _context.Entry(entity).State = EntityState.Deleted;
     _context.SaveChanges();
 }
Exemplo n.º 14
0
 public void Update(T item)
 {
     ctx.Entry(item).State = System.Data.Entity.EntityState.Modified;
     ctx.SaveChanges();
 }
Exemplo n.º 15
0
 public static void ForceRefresh <TEntity>([NotNull] this SystemDbContext thisValue, TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).State = EntityState.Detached;
 }
Exemplo n.º 16
0
 public static void ForceUpdate <TEntity>([NotNull] this SystemDbContext thisValue, TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).State = EntityState.Modified;
 }
Exemplo n.º 17
0
 public static void Reload <TEntity>([NotNull] this SystemDbContext thisValue, TEntity entity)
     where TEntity : class
 {
     thisValue.Entry(entity).Reload();
 }
 public void Insert(T entity)
 {
     db.Entry(entity).State = EntityState.Added;
     db.SaveChanges();
 }
Exemplo n.º 19
0
        public TEntity Update(TEntity entity)
        {
            _context.Entry(entity).State = EntityState.Modified;

            return(entity);
        }