Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dashing.IntegrationTests/Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,16 @@ public async Task InsertWorks(TestSessionWrapper wrapper) {
await wrapper.Session.InsertAsync(comment);
Assert.NotEqual(0, comment.CommentId);
}

[Theory]
[MemberData(nameof(SessionDataGenerator.GetSessions), MemberType = typeof(SessionDataGenerator))]
public async Task InsertOrUpdateWorks(TestSessionWrapper wrapper) {
const string CommentContent = "Foo InsertOrUpdate";
var comment = new Comment { Content = CommentContent };
await wrapper.Session.InsertOrUpdateAsync(comment);
await wrapper.Session.InsertOrUpdateAsync(comment);
var comments = await wrapper.Session.Query<Comment>().Where(c => c.Content == CommentContent).ToListAsync();
Assert.Equal(1, comments.Count);
}
}
}
18 changes: 12 additions & 6 deletions Dashing/Session.Extensions..cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ public int Delete<T>(params Expression<Func<T, bool>>[] predicates)
public int InsertOrUpdate<T>(T entity, Expression<Func<T, bool>> equalityComparer = null)
where T : class, new() {
if (equalityComparer == null) {
// if the equality comparer is null then they should be passing us a valid PK value in the entity so call update
var updated = this.Save(entity);
return updated == 0 ? this.Insert(entity) : updated;
// if the entity has previously come from the DB tracking will be enabled
if (((ITrackedEntity)entity).IsTrackingEnabled()) {
return this.Save(entity);
}

return this.Insert(entity);
}

// we support different equalityComparers so we can cope with e.g. username
Expand Down Expand Up @@ -317,9 +320,12 @@ public Task<int> DeleteAsync<T>(params Expression<Func<T, bool>>[] predicates)
public async Task<int> InsertOrUpdateAsync<T>(T entity, Expression<Func<T, bool>> equalityComparer = null)
where T : class, new() {
if (equalityComparer == null) {
// if the equality comparer is null then they should be passing us a valid PK value in the entity so call update
var updated = await this.SaveAsync(entity);
return updated == 0 ? await this.InsertAsync(entity) : updated;
// if the entity has previously come from the DB tracking will be enabled
if (((ITrackedEntity)entity).IsTrackingEnabled()) {
return await this.SaveAsync(entity);
}

return await this.InsertAsync(entity);
}

// we support different equalityComparers so we can cope with e.g. username
Expand Down