I'm new at entity framework, I was using Nhibernate. In 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;
}
}
That needs the id to update the entity. Is this the best approach? Is there some way to update the entity just by passing the entity without first finding it?