Skip to content

Instantly share code, notes, and snippets.

View johnknoop's full-sized avatar

John Knoop johnknoop

  • Gothenburg, Sweden
View GitHub Profile
@johnknoop
johnknoop / BulkUpsert.cs
Last active March 23, 2022 09:29
Upsert all in one db roundtrip
public async Task<IActionResult> Upsert(BulkUpsertRequest request)
{
request.Devices.ForEach(d =>
{
var device = new Device(d.Id, d.Name, d.Metadata.Select(...));
_dbContext.Entry(device).IsModified = true;
})
await _dbContext.SaveAsync();
}
namespace DeviceService.Api.Contracts;
public record DeviceMetadata(Guid MetadataFieldId, System.Text.Json.JsonElement Value);
public record DeviceToCreate(Guid DeviceTypeId, string DeviceName, IList<DeviceMetadata> MetaData);
public record BulkCreateDevicesRequest(IList<DeviceToCreate> Devices);
/*
Example: