Exemple #1
0
        public virtual void Update(TEntity entityToUpdate)
        {
            if (entityToUpdate == null)
            {
                throw new ArgumentException("Cannot add a null entity.");
            }
            var entry = Context.Entry <TEntity>(entityToUpdate);

            if (entry.State == EntityState.Detached)
            {
                var     set            = Context.Set <TEntity>();
                TEntity attachedEntity = set.Local.SingleOrDefault(e => e.Id == entityToUpdate.Id); // You need to have access to key

                if (attachedEntity != null)
                {
                    var attachedEntry = Context.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(entityToUpdate);
                }
                else
                {
                    entry.State = EntityState.Modified; // This should attach entity
                }
            }
        }
 public GenericRepository(GameStoreContext context)
 {
     this.context = context;
     this.dbSet   = context.Set <TEntity>();
 }
Exemple #3
0
 public GenericRepository(GameStoreContext context)
 {
     Context = context;
     DbSet   = context.Set <TEntity>();
 }