NickStrupat.EntityFrameworkCore.OnModelCreating 1.0.0

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

EntityFrameworkCore.OnModelCreating

An extension to EF Core that enables adding OnModelCreating to each entity so fluent model configuration can be kept in the entity class.

Overview

This library allows you to define Entity Framework Core model configuration directly within your entity classes, rather than centralizing all configuration in DbContext.OnModelCreating. This promotes better encapsulation and keeps entity configuration close to the entity definition.

Features

  • Define model configuration within entity classes using a static abstract interface method
  • Automatically discovers and invokes configuration for all registered entities
  • Type-safe configuration using generic constraints
  • Validates that entities implement the interface correctly
  • Works with EF Core 9.0+

Installation

dotnet add package NickStrupat.EntityFrameworkCore.OnModelCreating

Usage

1. Implement IModelCreating<T> on your entity

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NickStrupat.EntityFrameworkCore.OnModelCreating;

public class Product : IModelCreating<Product>
{
    public long Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    static void IModelCreating<Product>.OnModelCreating(EntityTypeBuilder<Product> builder)
    {
        builder.HasKey(p => p.Id);
        builder.Property(p => p.Name)
            .IsRequired()
            .HasMaxLength(200);
        builder.Property(p => p.Price)
            .HasPrecision(18, 2);
    }
}

2. Call OnModelCreatingEntities() in your DbContext

using Microsoft.EntityFrameworkCore;
using NickStrupat.EntityFrameworkCore.OnModelCreating;

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Invoke configuration from all entities implementing IModelCreating<T>
        modelBuilder.OnModelCreatingEntities();
    }
}

How It Works

  1. Your entity class implements IModelCreating<T> where T is the entity type itself
  2. The interface requires a static abstract method OnModelCreating that receives an EntityTypeBuilder<T>
  3. When you call modelBuilder.OnModelCreatingEntities(), the library:
    • Discovers all registered entity types in the model
    • Checks if each entity implements IModelCreating<T>
    • Invokes the static OnModelCreating method for each implementing entity
    • Validates that the generic type argument matches the entity type

Requirements

  • .NET 9.0 or later (uses static abstract interface members)
  • Entity Framework Core 9.0 or later

Benefits

  • Encapsulation: Keep entity configuration with the entity definition
  • Maintainability: Changes to an entity and its configuration are in the same file
  • Organization: Avoid massive OnModelCreating methods in DbContext
  • Type Safety: Generic constraints ensure configuration is applied to the correct entity
  • Discoverability: Configuration is easy to find when working on an entity

Error Handling

The library validates that entities implement IModelCreating<T> correctly. If an entity implements the interface with a different type argument than itself, an EntityImplementsInvalidModelCreatingException is thrown with details about the invalid implementation.

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 244 10/23/2025