CodeSync.Tools.EFExtensions 1.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package CodeSync.Tools.EFExtensions --version 1.0.0
                    
NuGet\Install-Package CodeSync.Tools.EFExtensions -Version 1.0.0
                    
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="CodeSync.Tools.EFExtensions" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeSync.Tools.EFExtensions" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CodeSync.Tools.EFExtensions" />
                    
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 CodeSync.Tools.EFExtensions --version 1.0.0
                    
#r "nuget: CodeSync.Tools.EFExtensions, 1.0.0"
                    
#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 CodeSync.Tools.EFExtensions@1.0.0
                    
#: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=CodeSync.Tools.EFExtensions&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CodeSync.Tools.EFExtensions&version=1.0.0
                    
Install as a Cake Tool

CodeSync.Tools.EFExtensions

Lightweight Entity Framework Core helpers for updating detached entities safely and predictably.

This package currently provides DbContextUpdateExtensions.AttachOrMerge, a focused extension that:

  • Attaches a detached entity to a DbContext and marks it for update (full overwrite), or
  • Merges values into an already-tracked instance with the same primary key, or
  • Allows partial updates by marking only selected properties as modified.

It intentionally does not traverse or update related entities/collections to avoid unexpected graph changes.

Requirements

  • .NET9
  • Entity Framework Core9.x (package references Microsoft.EntityFrameworkCore)

Installation

  • .NET CLI: dotnet add package CodeSync.Tools.EFExtensions
  • Package Manager: Install-Package CodeSync.Tools.EFExtensions

Quick start

using CodeSync.Tools.EFExtensions;
using Microsoft.EntityFrameworkCore;

// Full overwrite of all mapped scalar properties on the root entity
await using var db = new AppDbContext();
var detachedOrder = new Order { Id =123, Status = "Shipped", Notes = "Left at door" };

db.AttachOrMerge(detachedOrder); // fullOverwrite = true by default
await db.SaveChangesAsync();

API

public static class DbContextUpdateExtensions
{
 // If entity with same PK is tracked: copy scalar values onto it.
 // Else attach incoming instance. You can:
 // - Set fullOverwrite = true (default) to update all mapped scalar columns on the root.
 // - Set fullOverwrite = false and specify a whitelist of properties to mark as modified.
 public static void AttachOrMerge<TEntity>(this DbContext db,
 TEntity incoming,
 bool fullOverwrite = true,
 params Expression<Func<TEntity, object>>[] modifiedProps)
 where TEntity : class;
}

Behavior summary

  • Tracked instance exists (same primary key):
  • CurrentValues.SetValues(incoming) is applied to the tracked entity (scalar properties only).
  • Not tracked:
  • Entity is attached as Unchanged.
  • If fullOverwrite = true, the entity state is set to Modified (all mapped scalar columns on the root will be updated).
  • If fullOverwrite = false and modifiedProps provided, only those properties are marked as modified.
  • No navigation/collection traversal is performed.

Examples

###1) Full overwrite of root scalar properties

var product = new Product { Id =10, Name = "Widget X", Price =19.99m };
db.AttachOrMerge(product); // marks entire root as Modified (scalar columns)
await db.SaveChangesAsync();

###2) Partial update (selected properties only)

var customer = new Customer { Id =42, Email = "new@example.com", IsActive = true };

db.AttachOrMerge(customer,
 fullOverwrite: false,
 c => c.Email,
 c => c.IsActive);
await db.SaveChangesAsync();

###3) Merge into an already-tracked entity

// Suppose the context is already tracking a Customer with Id =42 (e.g., loaded earlier)
var incoming = new Customer { Id =42, Name = "Updated Name" };

db.AttachOrMerge(incoming); // copies scalar values onto the tracked instance
await db.SaveChangesAsync();

Notes and guidance

  • Primary key must be set on the incoming entity so the tracked instance can be found.
  • Only root entity scalar properties are updated; navigations/collections are ignored by design.
  • Suitable for HTTP PATCH/PUT patterns where you want predictable root updates without graph changes.
  • If you need graph updates, handle related entities explicitly per your domain rules.

License

MIT

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

Bug fixes