Entity Framework Core integration for the PhoneNumber value object, mapping it to a SQL PhoneNumber column type backed by VARCHAR(16) using a dedicated value converter.
$ dotnet add package PosInformatique.Foundations.PhoneNumbers.EntityFrameworkThis package provides Entity Framework Core integration for the PhoneNumber
value object from PosInformatique.Foundations.PhoneNumbers.
It allows you to map PhoneNumber properties to a database column of SQL type PhoneNumber
(backed by VARCHAR(16)), using a dedicated value converter.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.PhoneNumbers.EntityFramework
PhoneNumber value objectIsPhoneNumber() to configure propertiesPhoneNumber to a SQL column with type PhoneNumber (VARCHAR(16), non-Unicode)ValueConverter to convert between PhoneNumber and its E.164 string representationPhoneNumber in your EF Core entities without manual conversion logicPhoneNumber type mapped to VARCHAR(16))⚠️ To use
IsPhoneNumber(), you must first define the SQL typePhoneNumbermapped toVARCHAR(16)in your database. For SQL Server, you can create it with:
CREATE TYPE MimeType FROM VARCHAR(16) NOT NULL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PosInformatique.Foundations.PhoneNumbers;
public sealed class Customer
{
public int Id { get; set; }
public PhoneNumber Phone { get; set; } = default!;
}
public sealed class CustomerConfiguration : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.Property(c => c.Phone)
.IsPhoneNumber(); // Maps to SQL type PhoneNumber (VARCHAR(16))
}
}
The IsPhoneNumber() extension configures the property as:
IsUnicode(false))16PhoneNumber (which must be mapped in your database as VARCHAR(16))For example, your database column should look like:
Phone PhoneNumber NOT NULL
-- where `PhoneNumber` is mapped to VARCHAR(16)
Under the hood, the extension uses a ValueConverter<PhoneNumber, string>:
PhoneNumber is converted to its E.164 string representation (via ToString()).PhoneNumber instance.This ensures the database always stores the normalized E.164 value, while your code works with the strongly-typed PhoneNumber value object.