public TEntity Delete <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); } var deletedEntity = context.Set <TEntity>().Remove(entity); return(deletedEntity); } }
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); } }