Skip to content

Commit 6170790

Browse files
james-hesterNickCraver
authored andcommitted
Fix issue #418 for DeleteAsync (#1309)
Fix issue #418 for DeleteAsync (with tests)
1 parent 9282ef2 commit 6170790

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Dapper.Contrib/SqlMapperExtensions.Async.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,18 @@ public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T e
307307
throw new ArgumentException("Entity must have at least one [Key] or [ExplicitKey] property");
308308

309309
var name = GetTableName(type);
310-
keyProperties.AddRange(explicitKeyProperties);
310+
var allKeyProperties = keyProperties.Concat(explicitKeyProperties).ToList();
311311

312312
var sb = new StringBuilder();
313313
sb.AppendFormat("DELETE FROM {0} WHERE ", name);
314314

315315
var adapter = GetFormatter(connection);
316316

317-
for (var i = 0; i < keyProperties.Count; i++)
317+
for (var i = 0; i < allKeyProperties.Count; i++)
318318
{
319-
var property = keyProperties[i];
319+
var property = allKeyProperties[i];
320320
adapter.AppendColumnNameEqualsValue(sb, property.Name);
321-
if (i < keyProperties.Count - 1)
321+
if (i < allKeyProperties.Count - 1)
322322
sb.Append(" AND ");
323323
}
324324
var deleted = await connection.ExecuteAsync(sb.ToString(), entityToDelete, transaction, commandTimeout).ConfigureAwait(false);

Dapper.Tests.Contrib/TestSuite.Async.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public async Task TypeWithGenericParameterCanBeDeletedAsync()
8383
}
8484
}
8585

86+
[Fact]
87+
public async Task GetAsyncSucceedsAfterDeleteAsyncWhenExplicitKeyPresent()
88+
{
89+
using (var connection = GetOpenConnection())
90+
{
91+
await connection.DeleteAsync(new ObjectX { ObjectXId = Guid.NewGuid().ToString() }).ConfigureAwait(false);
92+
var retrieved = await connection.GetAsync<ObjectX>(Guid.NewGuid().ToString()).ConfigureAwait(false);
93+
Assert.Null(retrieved);
94+
}
95+
}
96+
8697
/// <summary>
8798
/// Tests for issue #351
8899
/// </summary>

0 commit comments

Comments
 (0)