Masterly.NonEmptyList.EntityFrameworkCore
3.0.0
See the version list below for details.
dotnet add package Masterly.NonEmptyList.EntityFrameworkCore --version 3.0.0
NuGet\Install-Package Masterly.NonEmptyList.EntityFrameworkCore -Version 3.0.0
<PackageReference Include="Masterly.NonEmptyList.EntityFrameworkCore" Version="3.0.0" />
<PackageVersion Include="Masterly.NonEmptyList.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Masterly.NonEmptyList.EntityFrameworkCore" />
paket add Masterly.NonEmptyList.EntityFrameworkCore --version 3.0.0
#r "nuget: Masterly.NonEmptyList.EntityFrameworkCore, 3.0.0"
#:package Masterly.NonEmptyList.EntityFrameworkCore@3.0.0
#addin nuget:?package=Masterly.NonEmptyList.EntityFrameworkCore&version=3.0.0
#tool nuget:?package=Masterly.NonEmptyList.EntityFrameworkCore&version=3.0.0
Masterly.NonEmptyList.EntityFrameworkCore
Entity Framework Core integration for Masterly.NonEmptyList. Provides value converters, comparers, and extension methods to seamlessly store and retrieve NonEmptyList<T> and ImmutableNonEmptyList<T> collections in your database.
Installation
dotnet add package Masterly.NonEmptyList.EntityFrameworkCore
Features
- JSON Value Conversion: Store
NonEmptyList<T>as JSON strings in the database - Change Tracking: Proper change detection with custom value comparers
- Relationship Support: Configure one-to-many and many-to-many relationships
- Validation Interceptor: Reject a null or empty non-empty property on save, including derived types such as
ConstrainedList; properties declared nullable may be null - Query Translation: Use NonEmptyList LINQ (
Select,OrderBy,Sum,Count,Head, ...) inside EF Core projections - Query Extensions: Convert query results to
NonEmptyList<T> - Auto-Configuration: Convention-based setup for all properties
Quick Start
Register once
services.AddDbContext<ShopContext>(options =>
options
.UseSqlServer(connectionString)
.UseNonEmptyList()); // SaveChanges validation + query translation
UseNonEmptyList() is required in v3 if you use LINQ on NonEmptyList<T> navigation properties inside queries. It rewrites the type-preserving operators back to Enumerable before EF Core compiles the query, so they translate exactly like List<T>.
var summaries = await context.Orders
.Select(o => new
{
o.Id,
Total = o.Items.Sum(i => i.Price),
Count = o.Items.Count,
FirstSku = o.Items.Head.Sku,
Names = o.Items.OrderBy(i => i.Price).Select(i => i.Name).ToList(),
})
.ToListAsync();
JSON Value Conversion
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public NonEmptyList<string> Tags { get; set; } = new("default");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.Property(p => p.Tags)
.HasNonEmptyListConversion();
}
One-to-Many Relationships
modelBuilder.Entity<Order>()
.HasNonEmptyManyWithOne(
o => o.Items,
i => i.Order,
i => i.OrderId);
Query Extensions
NonEmptyList<Product> products = await context.Products
.Where(p => p.IsActive)
.ToNonEmptyListAsync();
Caveat: Include on many-to-many navigations
Include works on one-to-many navigations configured with HasNonEmptyManyWithOne. On a many-to-many NonEmptyList<T> navigation it throws InvalidOperationException, because EF Core materializes the collection by creating it empty first. Query many-to-many data through a projection (Select) instead.
Documentation
Full guide: Entity Framework Core.
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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 is compatible. 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. |
-
net10.0
- Masterly.NonEmptyList (>= 3.0.0)
- Microsoft.EntityFrameworkCore (>= 10.0.0)
-
net8.0
- Masterly.NonEmptyList (>= 3.0.0)
- Microsoft.EntityFrameworkCore (>= 8.0.11)
-
net9.0
- Masterly.NonEmptyList (>= 3.0.0)
- Microsoft.EntityFrameworkCore (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v3.0.0:
- Built against Masterly.NonEmptyList 3.0 (composition-based NonEmptyList)
- Targets .NET 8, 9 and 10 with matching EF Core versions
v2.0.0:
- Initial release of Entity Framework Core integration
- NonEmptyListValueConverter and ImmutableNonEmptyListValueConverter for JSON serialization
- NonEmptyListValueComparer and ImmutableNonEmptyListValueComparer for change tracking
- HasNonEmptyListConversion and HasImmutableNonEmptyListConversion extension methods
- One-to-many relationship configuration: HasNonEmptyMany, HasNonEmptyManyWithOne
- Many-to-many relationship configuration: HasNonEmptyManyToMany
- Cascade delete and required navigation configuration
- SaveChanges validation interceptor via UseNonEmptyListValidation
- Query extensions: ToNonEmptyList, ToNonEmptyListAsync, ToNonEmptyListOrNull
- ApplyNonEmptyListConventions for auto-configuration
- Multi-targeting: .NET 8.0, 9.0 and 10.0