CoreOne.ModelPatch
1.2.2
See the version list below for details.
dotnet add package CoreOne.ModelPatch --version 1.2.2
NuGet\Install-Package CoreOne.ModelPatch -Version 1.2.2
<PackageReference Include="CoreOne.ModelPatch" Version="1.2.2" />
<PackageVersion Include="CoreOne.ModelPatch" Version="1.2.2" />
<PackageReference Include="CoreOne.ModelPatch" />
paket add CoreOne.ModelPatch --version 1.2.2
#r "nuget: CoreOne.ModelPatch, 1.2.2"
#:package CoreOne.ModelPatch@1.2.2
#addin nuget:?package=CoreOne.ModelPatch&version=1.2.2
#tool nuget:?package=CoreOne.ModelPatch&version=1.2.2
CoreOne.ModelPatch
A lightweight and flexible library for applying partial updates to entities in .NET Core and EF Core. Ideal for scenarios where PATCH operations are needed, such as RESTful APIs.
π Features
- Apply partial updates to EF Core entities using JSON Patch-like syntax
- Supports nested properties and complex object graphs
- Easy integration with ASP.NET Core Web APIs
- Reduces boilerplate code for update operations
- Respect unique index contrainsts while inserting objects
π¦ Installation
Install via NuGet:
dotnet add package CoreOne.ModelPatch
Or via the NuGet Package Manager:
Install-Package CoreOne.ModelPatch
π οΈ Usage
1. Define Your Entity
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string OtherField { get; set; }
public Address Location { get; set; }
public ICollection<Tag> Tags { get; set; }
}
public class Address
{
public string City { get; set; }
public string Country { get; set; }
}
[Index(nameof(Value), IsUnique = true)]
public class UserTag
{
public int Id { get; set; }
public int UserId { get; set; }
public string Value { get; set; }
public User? User { get; set; }
}
2. Create a Patch Model
var model = new User {
Name = "John",
Locaion = new Address {
City = "New York",
Country = "USA"
},
Tags = new List<Tag> {
new UserTag { Value = "tag1" } // this record is added
new UserTag { Value = "tag1" } // this one will be ignored
new UserTag { Value = "tag2" } // this record is added
}
};
3. Apply the Patch
Only fields present in the delta model are updated, if certain fields are not available then that field does not get the update.
var delta = model.ToDelta();
delta.Remove("OtherField");
var dataService = new DataModelService<YourContext>(IServiceProvider instance, yourContextInstance);
var result = await dataService.Patch(delta);
This will update the Name and Location.City properties of the User entity.
π§ͺ Sample API Endpoint
private readonly DataModelService _dataService;
public UserController(DataModelService dataService) => _dataService = dataService;
[HttpPatch("{id}")]
public async Task<IActionResult> PatchUser(int id, [FromBody] Delta<User> patchData, CancellationToken cancellationToken)
{
var result = await _dataService.Patch(delta, cancellationToken);
return Updated(result);
}
π Documentation
The library uses reflection to traverse and update properties. Itβs designed to be intuitive and extensible for most EF Core scenarios.
π€ Contributing
Feel free to fork the repo, submit issues, or open pull requests. Contributions are welcome!
π License
This project is licensed under the MIT License.
Would you like help writing unit tests or integrating this into a specific project?
| Product | Versions 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. |
-
net9.0
- CoreOne (>= 1.1.1)
- Microsoft.EntityFrameworkCore (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.