public TEntity Add <TEntity>(TEntity entity) where TEntity : class { if (entity == null) { return(null); } using (var context = new MailboxContext()) { var addedEntity = context.Set <TEntity>().Add(entity); context.SaveChanges(); return(addedEntity); } }
public TEntity Update <TEntity>(TEntity entity) where TEntity : class { if (entity == null) { return(null); } using (var context = new MailboxContext()) { // Make sure DbContext is tracking this entry var entry = context.Entry(entity); if (entry.State == EntityState.Detached) { context.Set <TEntity>().Attach(entity); } entry.State = EntityState.Modified; context.SaveChanges(); return(entity); } }