diff --git a/DapperExtensions/DapperImplementor.cs b/DapperExtensions/DapperImplementor.cs index 9c47a1e3..fdaa6809 100644 --- a/DapperExtensions/DapperImplementor.cs +++ b/DapperExtensions/DapperImplementor.cs @@ -286,10 +286,11 @@ protected bool Delete(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; } @@ -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 ? kvp.Value() : kvp.Value; - predicates.Add(fieldPredicate); + AddPredicates(predicateType, predicates, kvp.Key, kvp.Value is Func ? 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 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 includedProperties = null) { var parameters = new Dictionary();