What is the difference between Add, Update, Attach and Entry in EF Core?
Quick Answer
Add marks the entity (and its graph) Added → INSERT. Update marks it Modified → UPDATE of every column. Attach marks it Unchanged → nothing, until you change a property or set state. Entry(e).State = ... or Entry(e).Property(...).IsModified = true gives you fine control.
Add sets the state to Added so you get an INSERT. Update sets it to Modified — but the important detail is it marks every property modified, so the UPDATE writes all columns. Attach sets it to Unchanged, so it does nothing until you touch a property. And Entry lets me set the state or mark individual properties as modified. This matters in a Web API where the entity comes from the request body. I avoid Update there because it overwrites fields the client never sent and breaks concurrency checks. Instead I load the real entity, copy the allowed fields onto it, and save — then only the actual changes are written.
These matter in disconnected scenarios (a Web API receiving an entity from the request body, not loaded from the DB):
Updateis convenient but generatesSETfor every column — you overwrite fields the client didn't send, and it fights optimistic concurrency.- Safer pattern: load the existing entity, then copy the allowed fields onto it (
entity.Name = dto.Name; ...) andSaveChanges. Only changed columns are updated, concurrency tokens work, and you can't over-post. Attach+Entry(e).Property(x => x.Name).IsModified = trueupdates just that column without a round trip.- For deletes:
Removeon a tracked entity, orAttachthenRemovefor a disconnected one.