Exemple #1
0
        public void Edit(TEntity entity)
        {
            var entry = _context.Entry <TEntity>(entity);
            var pkey  = _dbSet.Create().GetType().GetProperty("id").GetValue(entity);

            if (entry.State == EntityState.Detached)
            {
                var     set            = _context.Set <TEntity>();
                TEntity attachedEntity = set.Find(pkey);  // access the key
                if (attachedEntity != null)
                {
                    var attachedEntry = _context.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(entity);
                }
                else
                {
                    entry.State = EntityState.Modified; // attach the entity
                }
            }

            _context.SaveChanges();
        }
Exemple #2
0
 public RepositoryBase(ContextDB context)
 {
     this._context = context;
     this._dbSet   = context.Set <TEntity>();
 }