Which is equivalent for Hibenate.saveOrUpdate () in the entity structure

In the entity infrastructure, you must write a lot of code to save or update a single object:

using (DataContext context = new DataContext()) { context.Task.Attach(task); if (task.ID == 0) { context.ObjectStateManager.ChangeObjectState(task, System.Data.EntityState.Added); } else { context.ApplyOriginalValues(task.GetType().Name, task); } context.SaveChanges(); } 

in sleep mode it's just saveOrUpdate()

It's not about laziness, but about making it short and clean.

+4
source share
1 answer

There is no equivalent. You really need to write this as:

 using (DataContext context = new DataContext()) { context.Task.Attach(task); if (task.ID == 0) { context.ObjectStateManager.ChangeObjectState(task, System.Data.EntityState.Added); } else { context.ObjectStateManager.ChangeObjectState(task, System.Data.EntityState.Modified); } context.SaveChanges(); } 
+4
source

All Articles