Skip to content

Commit

Permalink
fix DapperExtensions.Get will occur exception when parameter id is no…
Browse files Browse the repository at this point in the history
…t a KeyValuePair (issue tmsmith#315)
  • Loading branch information
noneiori committed Jul 16, 2023
1 parent ae74414 commit c98ee37
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions DapperExtensions/DapperImplementor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,11 @@ protected bool Delete<T>(IDbConnection connection, IClassMapper classMap, IPredi
protected static IPredicate GetPredicate(IClassMapper classMap, object predicate)
{
var wherePredicate = predicate as IPredicate;

if (wherePredicate == null && predicate != null)
{
wherePredicate = GetEntityPredicate(classMap, predicate);
}
}

return wherePredicate;
}
Expand Down Expand Up @@ -367,17 +368,30 @@ protected static IPredicate GetEntityPredicate(IClassMapper classMap, object ent
var notIgnoredColumns = classMap.Properties.Where(p => !p.Ignored);
foreach (var kvp in ReflectionHelper.GetObjectValues(entity).Where(property => notIgnoredColumns.Any(c => c.Name == property.Key)))
{
var fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate;
fieldPredicate.Not = false;
fieldPredicate.Operator = Operator.Eq;
fieldPredicate.PropertyName = kvp.Key;
fieldPredicate.Value = kvp.Value is Func<object> ? kvp.Value() : kvp.Value;
predicates.Add(fieldPredicate);
AddPredicates(predicateType, predicates, kvp.Key, kvp.Value is Func<object> ? kvp.Value() : kvp.Value);
}

// predicates will be empty if entity is not KeyValuePair
if (entity != null && !predicates.Any())
{
//Get Primary Key when use Identity
var key = classMap.Properties.SingleOrDefault(p=>p.KeyType == KeyType.Identity);
AddPredicates(predicateType, predicates, key.Name, entity);
}

return ReturnPredicate(predicates);
}

private static void AddPredicates(Type predicateType, IList<IPredicate> predicates, string key, object value)
{
var fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate;
fieldPredicate.Not = false;
fieldPredicate.Operator = Operator.Eq;
fieldPredicate.PropertyName = key;
fieldPredicate.Value = value;
predicates.Add(fieldPredicate);
}

protected GridReaderResultReader GetMultipleByBatch(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout, IList<IReferenceMap> includedProperties = null)
{
var parameters = new Dictionary<string, object>();
Expand Down

0 comments on commit c98ee37

Please sign in to comment.