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
<PackageReference Include="NickStrupat.EntityFrameworkCore.OnModelCreating" Version="1.0.0" />
<PackageVersion Include="NickStrupat.EntityFrameworkCore.OnModelCreating" Version="1.0.0" />
<PackageReference Include="NickStrupat.EntityFrameworkCore.OnModelCreating" />
paket add NickStrupat.EntityFrameworkCore.OnModelCreating --version 1.0.0
#r "nuget: NickStrupat.EntityFrameworkCore.OnModelCreating, 1.0.0"
#:package NickStrupat.EntityFrameworkCore.OnModelCreating@1.0.0
#addin nuget:?package=NickStrupat.EntityFrameworkCore.OnModelCreating&version=1.0.0
#tool nuget:?package=NickStrupat.EntityFrameworkCore.OnModelCreating&version=1.0.0
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
- Your entity class implements
IModelCreating<T>whereTis the entity type itself - The interface requires a static abstract method
OnModelCreatingthat receives anEntityTypeBuilder<T> - 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
OnModelCreatingmethod 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
OnModelCreatingmethods 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 | Versions 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. |
-
net9.0
- Microsoft.EntityFrameworkCore (>= 9.0.10)
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 |