I'm having problems in the following line of code:
var key = entity.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("id")).GetValue(this, null);
Whenever this line arrives it throws an exception
The object does not match the target type.
The whole method is this:
public void AddOrUpdate(TEntity entity)
{
var key = entity.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("id")).GetValue(this, null);
if (entity == null)
throw new ArgumentNullException("entity");
if ((int)key == 0)
{
_entities.Set<TEntity>().Add(entity);
}
else
{
_entities.Entry(entity).State = EntityState.Modified;
}
}