i am new in EF. i need to add data if it does not exist in db else update data.i got a code but could not understand how to call it. so need a small example which tell me how could i use AddOrModify to add or update my employee data.
does the below code do the db round trip to check the data exist or not?
how to make the below code extension method?
public void AddOrModify<T>(T entity, string key) where T : class, IEntity
{
using (var context = new MyContainer())
{
if (context.Set<T>().Any(e => e.MyKey == key))
{
context.Entry(entity).State = EntityState.Modified;
}
else
{
context.Entry(entity).State = EntityState.Added;
}
context.SaveChanges();
}
}
looking for help with sample code.