I'm trying to pull data from a rest endpoint and saving it into a database. However I want to update a record if it already exists, or create it when it doesn't exist yet.
I have the following code:
private void Sync()
{
using (var db = new context())
{
// get data from rest endpoint
foreach (var item in array)
{
// create entity
var myEntity = new MyEntity();
// Method 1
db.Entry(myEntity).State = EntityState.Modified;
// Method 2
db.myEntities.AddOrUpdate(e => new { e.att1, e.att2, }, myEntity);
}
db.SaveChanges();
}
}
Which method would work better for the behaviour I want, if at all?