I\'m trying to accomplish something really simple and I can\'t find how to do it using Entity Framework 4.1.
I want a controller method that accepts an object and
Unfortunately there is no way to do this without querying database or using stored procedure. The minimalistic code should be:
public void AddOrModify(T entity, string key) where T : class, IEntity // Implements MyKey
{
using (var context = new MyContainer())
{
if (context.Set().Any(e => e.MyKey == key))
{
context.Entry(entity).State = EntityState.Modified;
}
else
{
context.Entry(entity).State = EntityState.Added;
}
context.SaveChanges();
}
}