FlexLabs.EntityFrameworkCore.Upsert
11.0.0
Prefix Reserved
dotnet add package FlexLabs.EntityFrameworkCore.Upsert --version 11.0.0
NuGet\Install-Package FlexLabs.EntityFrameworkCore.Upsert -Version 11.0.0
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="11.0.0" />
<PackageVersion Include="FlexLabs.EntityFrameworkCore.Upsert" Version="11.0.0" />
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" />
paket add FlexLabs.EntityFrameworkCore.Upsert --version 11.0.0
#r "nuget: FlexLabs.EntityFrameworkCore.Upsert, 11.0.0"
#:package FlexLabs.EntityFrameworkCore.Upsert@11.0.0
#addin nuget:?package=FlexLabs.EntityFrameworkCore.Upsert&version=11.0.0
#tool nuget:?package=FlexLabs.EntityFrameworkCore.Upsert&version=11.0.0
FlexLabs.Upsert
Upsert support for EF Core: insert a row, or update it if it already exists, in a single database command.
EF Core has no built-in way to do this. The usual workaround is to query first and then insert or update, which costs an extra round trip and breaks down when two requests race each other. This library generates the native upsert statement for your database instead, so the check and the write happen atomically.
| Database | Generated statement |
|---|---|
| PostgreSQL, SQLite | INSERT … ON CONFLICT DO UPDATE |
| SQL Server, Oracle | MERGE |
| MySQL, MariaDB | INSERT … ON DUPLICATE KEY UPDATE |
| In-memory | Emulated, for tests |
Getting started
dotnet add package FlexLabs.EntityFrameworkCore.Upsert
The package's major version follows EF Core's: 11.x is built for EF Core 11, 10.x for EF Core 10, and so on.
The provider is picked up from your DbContext configuration, so there is nothing to register.
Then, with using Microsoft.EntityFrameworkCore; in scope:
await db.DailyVisits
.Upsert(new DailyVisit
{
UserID = userID,
Date = DateTime.UtcNow.Date,
Visits = 1,
})
.On(v => new { v.UserID, v.Date })
.WhenMatched(v => new DailyVisit
{
Visits = v.Visits + 1,
})
.RunAsync();
If there is no visit for that user and date, a new row is inserted. If there is one, its Visits column is
incremented, in SQL, without reading the current value first.
What you can do
// Match on a unique key, then update everything else with the new values
db.Countries.Upsert(country).On(c => c.ISO).Run();
// Upsert many rows at once
db.Countries.UpsertRange(countries).On(c => c.ISO).Run();
// Keep some columns untouched when the row already exists
db.Countries.Upsert(country).On(c => c.ISO).Exclude(c => c.Created).Run();
// Insert only, leave existing rows alone
db.Countries.Upsert(country).On(c => c.ISO).NoUpdate().Run();
// Only update when a condition holds
db.Products.Upsert(product).On(p => p.Sku)
.UpdateIf((existing, incoming) => incoming.Version > existing.Version)
.Run();
// Get the resulting entities back (PostgreSQL, SQL Server, SQLite)
var saved = await db.Countries.Upsert(country).On(c => c.ISO).RunAndReturnAsync();
Things to know before you start:
- The
On()columns must be a primary key or have a unique index, and can't be database-generated. If you leaveOn()out, the primary key is used. - Upserts run immediately, straight against the database.
SaveChanges()isn't involved, and nothing is added to the change tracker unless you useRunAndReturn(). - Databases limit how many parameters a command can take, so a large
UpsertRangeis split into several commands. That keeps it working, but it is still one parameter per column per row, so it isn't a replacement for a bulk loader.
Documentation
- Usage guide: every method, with examples
- Matching rows: choosing the
On()columns - Update expressions: what you can write in
WhenMatched()andUpdateIf() - Default values: how
HasDefaultValue()and sentinels are handled - Owned types and JSON
- Database providers: per-database differences and limitations, custom runners
Contributing
Issues and pull requests are welcome. See test/README.md for how to run the tests.
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Microsoft.EntityFrameworkCore.Relational (>= 11.0.0 && < 12.0.0)
NuGet packages (18)
Showing the top 5 NuGet packages that depend on FlexLabs.EntityFrameworkCore.Upsert:
| Package | Downloads |
|---|---|
|
HwApp.Core
HwApp Core |
|
|
Elsa.Persistence.EntityFrameworkCore
Elsa is a set of workflow libraries and tools that enable super-fast workflowing capabilities in any .NET Core application. This package provides an Entity Framework Core persistence provider. |
|
|
QuantFi.Services
Package Description |
|
|
OnXap.Core
Ядро приложения OnXap. |
|
|
YYApp
YYApp Core |
GitHub repositories (4)
Showing the top 4 popular GitHub repositories that depend on FlexLabs.EntityFrameworkCore.Upsert:
| Repository | Stars |
|---|---|
|
Arcenox-co/TickerQ
TickerQ is a fast, reflection-free background task scheduler for .NET built with source generators, EF Core integration, cron + time-based execution, and a real-time dashboard.
|
|
|
Texnomic/SecureDNS
Secure, Modern, Fully-Featured, All-In-One Cross-Architecture & Cross-Platform DNS Server Using .NET 10
|
|
|
daniel3303/Equibles
Self-hosted, open-source financial data MCP server for AI agents — SEC filings, XBRL financials, 13F holdings, insider & congressional trades, short interest, FRED, CFTC/CBOE and daily prices across 64 MCP tools. Equibles Cloud adds earnings call transcripts, live quotes, options chains with Greeks, guidance and a screener.
|
|
|
Kukks/NNostr
A Nostr Relay and Client written in C#
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 11.0.0 | 4,620 | 11/15/2026 | |
| 10.1.0 | 375,149 | 8/11/2026 | |
| 10.0.0 | 1,688,741 | 2/26/2026 | |
| 10.0.0-rc1 | 53,381 | 2/3/2026 | |
| 10.0.0-beta2 | 83,919 | 12/23/2025 | |
| 10.0.0-beta1 | 368,780 | 11/5/2025 | |
| 9.103.0 | 415,867 | 1/21/2026 | |
| 9.102.0 | 32,020 | 12/23/2025 | |
| 9.101.0 | 103,075 | 11/4/2025 | |
| 9.100.1 | 177,284 | 9/17/2025 | |
| 9.100.0 | 65,427 | 8/30/2025 | |
| 9.0.0 | 686,356 | 3/3/2025 | |
| 8.1.2 | 1,470,167 | 11/28/2024 | |
| 8.1.0 | 49,656 | 11/24/2024 | |
| 8.0.0 | 3,184,975 | 12/3/2023 | |
| 7.0.0 | 1,366,716 | 12/18/2022 | |
| 7.0.0-beta.2 | 6,871 | 11/22/2022 | |
| 7.0.0-beta.1 | 637 | 11/16/2022 | |
| 6.0.2 | 695,247 | 11/16/2022 |
v11.0.0
+ Adding support for EF Core 11
+ Allow nullable properties in the On() and Exclude() expressions (Thanks to @Dreamescaper)
! Use configured default values (HasDefaultValue / HasDefaultValueSql) when an entity property holds its sentinel value, not just null (Thanks to @negrime)
! Known limitation: sequence based HasDefaultValueSql() defaults aren't supported in Oracle when upserting multiple entities
! BREAKING: Properties that equal their sentinel value (e.g. 0 for an int with a different default value configured in EF/DB) are now replaced by the configured default, which can change the values upserted
v10.1.0
* Support more MySql database engines (Thanks to @0xced)
v10.0.0
+ Adding support for EF Core 10
v9.102.0
! Fix type mapping for ulong constants (Thanks to @ahmednfwela)
! Fix RunAndReturn methods returning stale tracked entity values (Thanks to @sdukehart-omnesoft)
v9.101.0
! Patching implicit conversions not being supported after last update (Thanks to @fortender)
! Patching query filters preventing from retrieving upserted entities (Thanks to @Lexiphan)
v9.100.0
+ Significant internal upgrade with a new expression parser (Thanks to @r-Larch)
+ Adding support for owned entities and owned JSON entities (Thanks to @r-Larch)
+ Adding option to exclude columns on auto-updates (Thanks to @B3RR10)
v9.0.0
+ Adding support for EF Core 9
v8.1.0
+ Adding initial support for Oracle DB! (Thanks to @dadyarri)
+ Adding test support for returning inserted objects (Thanks to @PhenX)
+ Adding support for upserting into views (ymmv)
! Patching argument count calculation (for max argument count handling)
! Patching null constant handling in the update condition
v8.0.0
+ Adding support for EF Core 8