public Rsvp Create(Rsvp rsvp) { // validation if (string.IsNullOrWhiteSpace(rsvp.Email)) { throw new AppException("Email is required"); } Rsvp existing = GetByEmailAndECardId(rsvp.Email, rsvp.Id_EcardDetail); if (existing == null) { // check if local is not null if (existing != null) // I'm using a extension method { // detach _context.Entry(existing).State = EntityState.Detached; } _context.Rsvp.Add(rsvp); _context.SaveChanges(); } else { Update(rsvp); } return(rsvp); }
public void Update(T entity) { var oriEntity = GetById(entity.Id); if (oriEntity == null) { throw new RecordNotFoundException($"Entity record with Id {entity.Id} cannot find."); } _context.Entry(oriEntity).CurrentValues.SetValues(entity); _context.Entry(oriEntity).Property(x => x.CreatedDate).IsModified = false; _context.SaveChanges(); }