Provides Entity Framework Core integration for the MimeType value object. Enables seamless mapping of MIME types as strongly-typed properties in Entity Framework Core entities, with safe conversion to string.
$ dotnet add package PosInformatique.Foundations.MediaTypes.EntityFrameworkProvides Entity Framework Core integration for the MimeType value object from
PosInformatique.Foundations.MediaTypes.
This package enables seamless mapping of MIME types as strongly-typed properties in Entity Framework Core entities.
It ensures proper SQL type mapping, validation, and conversion to VARCHAR(128) when persisted to the database.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.MediaTypes.EntityFramework
This package depends on the base package PosInformatique.Foundations.MediaTypes.
IsMimeType() to configure EF Core properties for MimeType.VARCHAR(128) database columns using the SQL type MimeType (you must define the SQL type MimeType mapped to VARCHAR(128) in your database).MimeType value object.⚠️ To use
IsMimeType(), you must first define the SQL typeMimeTypemapped toVARCHAR(128)in your database. For SQL Server, you can create it with:
CREATE TYPE MimeType FROM VARCHAR(128) NOT NULL;
using Microsoft.EntityFrameworkCore;
using PosInformatique.Foundations.MediaTypes;
public class Document
{
public int Id { get; set; }
public MimeType ContentType { get; set; }
}
public class ApplicationDbContext : DbContext
{
public DbSet<Document> Documents => Set<Document>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Document>()
.Property(d => d.ContentType)
.IsMimeType();
}
}
This will configure the ContentType property of the Document entity with:
VARCHAR(128) (non-unicode) column lengthMimeTypeMimeType and string