public virtual T Save(T entity) { entity.PrepareSave(); if (entity.Id == 0) { contextBD.Set <T>().Add(entity); contextBD.Entry(entity).State = EntityState.Added; } else { contextBD.Set <T>().Attach(entity); contextBD.Entry(entity).State = EntityState.Modified; } contextBD.SaveChanges(); return(entity); }