I want to write a function, which receives any Entity
object and finds the current value by automatically determined primary key and updates it.
Could you please point me any direction.
public void Update(object entity)
{
using (var _db = new MyEntities())
{
var table = _db.Set(entity.GetType());
//Here we should somehow find the entity object which primary key coincides with the one of entity
var entityObj = table.FindObjectByPrimaryKey(entity);
entityObj.CopyDataFrom(entity);
_db.SaveChanges()
}
}
Is this possible?
you can find primary keys of an entity like this
and then use reflection to access their values.