This 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- Entity Framework Core support for the
PhoneNumbervalue object - Simple extension method
IsPhoneNumber()to configure properties - Maps
PhoneNumberto a SQL column with typePhoneNumber(VARCHAR(16), non-Unicode) - Uses a
ValueConverterto convert betweenPhoneNumberand its E.164 string representation
- Persist
PhoneNumberin your EF Core entities without manual conversion logic - Keep strong typing in your domain model while storing normalized E.164 strings in the database
- Enforce a consistent database schema for phone numbers (custom
PhoneNumbertype mapped toVARCHAR(16))
⚠️ To useIsPhoneNumber(), 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:
- Non-Unicode (
IsUnicode(false)) - Maximum length:
16 - Column type:
PhoneNumber(which must be mapped in your database asVARCHAR(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>:
- When saving,
PhoneNumberis converted to its E.164 string representation (viaToString()). - When loading, the stored string is parsed back to a
PhoneNumberinstance.
This ensures the database always stores the normalized E.164 value, while your code works with the strongly-typed PhoneNumber value object.