LarchSys.FlexLabs.Upsert 9.1.0-preview-1

This is a prerelease version of LarchSys.FlexLabs.Upsert.
dotnet add package LarchSys.FlexLabs.Upsert --version 9.1.0-preview-1
                    
NuGet\Install-Package LarchSys.FlexLabs.Upsert -Version 9.1.0-preview-1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LarchSys.FlexLabs.Upsert" Version="9.1.0-preview-1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LarchSys.FlexLabs.Upsert" Version="9.1.0-preview-1" />
                    
Directory.Packages.props
<PackageReference Include="LarchSys.FlexLabs.Upsert" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LarchSys.FlexLabs.Upsert --version 9.1.0-preview-1
                    
#r "nuget: LarchSys.FlexLabs.Upsert, 9.1.0-preview-1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LarchSys.FlexLabs.Upsert@9.1.0-preview-1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LarchSys.FlexLabs.Upsert&version=9.1.0-preview-1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=LarchSys.FlexLabs.Upsert&version=9.1.0-preview-1&prerelease
                    
Install as a Cake Tool

GitHub Actions Workflow Status NuGet Version

Install Nuget

> dotnet add package LarchSys.FlexLabs.Upsert --prerelease

This is a fork of artiomchi/FlexLabs.Upsert

This fork provides support for Owned entities and Owned JSON entities.

Owned Entities

using var dbContext = new TestDbContext(_fixture.DataContextOptions);

var newParent = new Parent
{
    ID = 1,
    Child = new Child
    {
        ChildName = "Someone else",
        Age = 10,
        SubChild = new SubChild
        {
            SubChildName = "SubChild foobar",
            Age = 10,
        }
    },
};

dbContext.Parents.Upsert(newParent)
    .On(p => p.ID)
    .WhenMatched((a, b) => new Parent
    {
        Counter = b.Counter + 1,
        Child = new Child
        {
            SubChild = b.Child.SubChild, // nested owned direct mapping - should expand to all columns.
        }
    })
    .Run();

Owned JSON Entities

using var dbContext = new TestDbContext(_fixture.DataContextOptions);

var company = new CompanyOwnedJson
{
    Id = 1,
    Name = "Company 1",
    Meta = new CompanyMeta // .OwnsOne(_ => _.Meta, _ => _.ToJson()...)
    {
        Required = "required-value",
        JsonOverride = "col with [JsonPropertyName]",
        Nested = new CompanyNestedMeta
        {
            Title = "I'm a nested json",
        },
        Properties = [
            new CompanyMetaValue {
                Key = "foo",
                Value = "bar",
            },
            new CompanyMetaValue {
                Key = "cat",
                Value = "dog",
            }
        ],
    }
};

dbContext.CompanyOwnedJson.Upsert(company)
    .On(p => p.Id)
    .WhenMatched((a, b) => new CompanyOwnedJson
    {
        Name = b.Name,
        Meta = b.Meta, // assigning a JSON is supported.
    })
    .Run();

FlexLabs.Upsert

Build status FlexLabs.EntityFrameworkCore.Upsert on NuGet
CI build: FlexLabs.EntityFrameworkCore.Upsert on MyGet

This library adds basic support for "Upsert" operations to EF Core.

Uses INSERT … ON CONFLICT DO UPDATE in PostgreSQL/Sqlite, MERGE in SqlServer & Oracle and INSERT INTO … ON DUPLICATE KEY UPDATE in MySQL.

Also supports injecting sql command runners to add support for other providers

A typical upsert command could look something like this:

DataContext.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();

In this case, the upsert command will ensure that a new DailyVisit will be added to the database. If a visit with the same UserID and Date already exists, it will be updated by incrementing it's Visits value by 1.

Please read our Usage page for more examples

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.1.0-preview-1 280 5/5/2025

v9.1.0-preview-1
+ Adding support for Owned entities and Owned JSON entities

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

v7.0.0
+ Adding support for EF Core 7

v6.0.2
* Improving entity type detection when using DbSet<>
! Patching MySQL handling of null columns

v6.0.0
+ Adding support for EF Core 6
+ Handling UseIdentityAlwaysColumn columns

v5.0.0
! Fixing the library versioning. From now one, one version of the library depends on one version of EF Core, for all supported target frameworks

v4.0.1
! Patching some MySql conditional update queries

v4.0.0
+ Adding support for .NET 5 and EF Core 5
! Patched support for constants in the update condition
! Run and RunAsync will not return all rows affected when command was split into multiple batches, not just the last batch's row count
! Patching the extension method to replace/inject custom command runner
! Removed old extension method on IServiceCollection, since it wasn't working anyway