Entity Framework Core: insert or update, the lazy way
www.corstianboerman.com › blog › 2019/03/30Mar 30, 2019 · I've been fiddling around quite a lot with GraphQL lately. During this process I figured out that the create and update logic I write on the client-side is quite simillar. This led me to think that, unless a special operation needed to be executed, most create and update logic could be combined in save, or in my case mutate operation. Depending on whether the primary key would be provided either an update or create operation should be executed.
Best way to update an entity in entity Framework [duplicate]
stackoverflow.com › questions › 28932621In Nhibernate to update an object it's not necessary to pass the id, you just pass the entity and Nhibernate matches the id by itself and update the entity. In EF I'm using this approach: protected virtual bool UpdateEntity (TEntity entity, int id) { using (var ctx = new GenericContext ()) { var list = ctx.Set<TEntity> ().ToList (); ctx.Entry<TEntity> (ctx.Set<TEntity> ().Find (id)).CurrentValues.SetValues (entity); return ctx.SaveChanges () > 0; } }
Add or Update in EF6 Tutorial - Entity Framework
entityframework.net › add-or-updateEntity Framework AddOrUpdate. AddOrUpdate. add-or-update. A common pattern for some applications is to either add an entity as new (resulting in a database insert) or attach an entity as existing and mark it as modified (resulting in a database update) depending on the value of the primary key. For example, when using database generated integer primary keys it is common to treat an entity with a zero key as new and an entity with a non-zero key as existing.