Masterly.NonEmptyList.EntityFrameworkCore 3.0.1

dotnet add package Masterly.NonEmptyList.EntityFrameworkCore --version 3.0.1
                    
NuGet\Install-Package Masterly.NonEmptyList.EntityFrameworkCore -Version 3.0.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="Masterly.NonEmptyList.EntityFrameworkCore" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Masterly.NonEmptyList.EntityFrameworkCore" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Masterly.NonEmptyList.EntityFrameworkCore" />
                    
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 Masterly.NonEmptyList.EntityFrameworkCore --version 3.0.1
                    
#r "nuget: Masterly.NonEmptyList.EntityFrameworkCore, 3.0.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 Masterly.NonEmptyList.EntityFrameworkCore@3.0.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=Masterly.NonEmptyList.EntityFrameworkCore&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Masterly.NonEmptyList.EntityFrameworkCore&version=3.0.1
                    
Install as a Cake Tool

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 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. 
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
3.0.1 93 11/2/2026
3.0.0 90 11/2/2026
2.0.0 195 1/2/2026

v3.0.1:
- New package icon: a pair of brackets around a single element

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