BritishPrimitives.EntityFramework 1.0.0

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

NuGet Version NuGet Downloads GitHub License

British Primitives

Introduction

BritishPrimitives is a .NET library that provides a set of primitive types for representing common UK-specific data formats. These types are designed to be lightweight and efficient, with a focus on performance and ease of use. The library also includes support for serialization and Entity Framework Core, making it easy to use in a variety of applications.

The following table lists the types included in the library, along with their sizes in memory:

Type Size (bytes)
CompanyRegistrationNumber 6
NationalInsuranceNumber 5
PostalCode 8

Installation

You can install the library through the NuGet Package Manager:

Install-Package BritishPrimitives

Validation & Initialization

All of the primitive types in this library can be initialized in a similar way. For example, you can create a PostalCode from a string, and the library will validate the format for you:

// Valid postcode
var postcode = PostalCode.Parse("SW1A 0AA");

// Invalid postcode - throws FormatException
var invalidPostcode = PostalCode.Parse("not a postcode");

You can also use the TryParse method to avoid exceptions:

if (PostalCode.TryParse("SW1A 0AA", out var postcode))
{
    // ...
}

Serialization

All of the structs in this library can be marshaled directly into bytes. They also provide explicit conversions to and from ulong for easy binary serialization:

var postcode = PostalCode.Parse("SW1A 0AA");
ulong value = (ulong)postcode;

JSON

The BritishPrimitives.Json library provides converters for serializing and deserializing the primitive types to and from JSON. You can serialize them as either strings or integers.

Installation

You can install the library through the NuGet Package Manager:

Install-Package BritishPrimitives.Json

Usage

You can use the converters in two main ways: either by applying the [JsonConverter] attribute directly to a property or by adding a converter factory to JsonSerializerOptions.

Using Converters Directly with attributes

For individual properties, you can apply the JSON converter attribute with either PrimitiveStringConverter<T> or PrimitiveIntegerConverter<T>.

This converter will serialize the primitive type as a JSON string.

using BritishPrimitives.Json;
using System.Text.Json.Serialization;

public class MyModel
{
    [JsonConverter(typeof(PrimitiveStringConverter<PostalCode>))]
    public PostalCode Postcode { get; set; }
}
Using Converter Factories

For a more general approach, you can use PrimitiveStringConverterFactory or PrimitiveIntegerConverterFactory to handle all primitive types in your model.

PrimitiveStringConverterFactory

This factory will create string converters for all types that implement IPrimitive<T>.

var options = new JsonSerializerOptions
{
    Converters = { new PrimitiveIntegerConverterFactory() }
};

var model = new MyModel { Crn = CompanyRegistrationNumber.Parse("12345678") };
var json = JsonSerializer.Serialize(model, options);

Entity Framework

The BritishPrimitives.EntityFramework library provides value converters for Entity Framework Core. You can use these converters to store the primitive types in your database as either strings or integers.

Installation

You can install the library through the NuGet Package Manager:

Install-Package BritishPrimitives.EntityFramework

Usage

The Model
using BritishPrimitives.EntityFramework;

public class Address
{
    public int Id { get; set; }
    public PostalCode Postcode { get; set; }
}
The DbContext
using BritishPrimitives.EntityFramework;

public class MyDbContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("your_connection_string");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>()
            .Property(e => e.Postcode)
            .HasConversion<PostalCodeStringConverter>();
    }
}
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
1.0.0 164 10/17/2025
1.0.0-rc 126 10/3/2025