Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ jobs:
- uses: actions/checkout@v7

# One SDK per target framework: each TFM builds and tests against its own EF Core major
# (8, 9, 10), and the tests need that runtime to run.
# (8, 9, 10, 11), and the tests need that runtime to run. The newest SDK builds them all.
- uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
11.0.x

- name: Restore
run: dotnet restore
Expand Down Expand Up @@ -67,9 +68,13 @@ jobs:
steps:
- uses: actions/checkout@v7

# The .NET 11 SDK too: restoring the library, which this project references, includes
# its net11.0 target, which the .NET 10 SDK can't build.
- uses: actions/setup-dotnet@v5
with:
dotnet-version: 10.0.x
dotnet-version: |
10.0.x
11.0.x

- name: Integration tests
run: dotnet test --project tests/Hlibz.EntityFrameworkCore.ModelRules.IntegrationTests --configuration Release
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
8.0.x
9.0.x
10.0.x
11.0.x

- name: Restore
run: dotnet restore
Expand Down
21 changes: 13 additions & 8 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ builds the model (`UseModelRules` in `ConfigureConventions`) or from a test

## Requires

* .NET 10 SDK, plus the .NET 8 and 9 runtimes to run every test target.
* .NET 11 SDK, plus the .NET 8, 9 and 10 runtimes to run every test target.

## Commands

```bash
# Build everything
dotnet build

# Run all tests (all three target frameworks)
# Run all tests (all four target frameworks)
dotnet test --project tests/Hlibz.EntityFrameworkCore.ModelRules.Tests

# One target framework only
Expand All @@ -44,12 +44,17 @@ Testcontainers 4.15 talks Docker API 1.44+. With an older local Docker Engine (e
## Architecture

- **Multi-targeting = one EF Core major per TFM.** `Directory.Build.props` sets
`net8.0;net9.0;net10.0` for every project, and `Directory.Packages.props` pins
`Microsoft.EntityFrameworkCore.Relational` (and the test-only Npgsql provider) per TFM:
8.0.x on net8.0, 9.0.x on net9.0, 10.0.x on net10.0. So each build of the library is compiled
and tested against its own EF major, and the package's dependency group for each TFM pulls the
matching EF. Use `#if NET10_0_OR_GREATER` for APIs that only exist in newer EF majors (see
`ModelWalker.IsJson` and the JSON complex-type container column in `ModelIdentifiers`).
`net8.0;net9.0;net10.0;net11.0` for every project, and `Directory.Packages.props` pins
`Microsoft.EntityFrameworkCore.Relational` (and the test-only providers) per TFM: 8.0.x on
net8.0, 9.0.x on net9.0, 10.0.x on net10.0, 11.0.x on net11.0. On net11.0 the Npgsql and
Oracle MySQL providers are still prereleases (test-only, so that's fine); the test project
suppresses NU1608 because Npgsql's rc pins EF Core to its own rc. So each build of the
library is compiled and tested against its own EF major, and the package's dependency group
for each TFM pulls the matching EF. Use `#if NET10_0_OR_GREATER` for APIs that only exist in
newer EF majors (see `ModelWalker.IsJson` and the JSON complex-type container column in
`ModelIdentifiers`). From EF Core 11 `IReadOnlyIndex.Properties` is a list of
`IReadOnlyPropertyBase` (an index can hold a JSON complex property), so take index members as
`IEnumerable<IReadOnlyPropertyBase>`.
- **The Relational pin is the package's minimum EF Core version.** Its version becomes the
`>=` floor in the published package's dependency group for that TFM, so raising it forces every
consumer onto that patch. A consumer whose project references `Microsoft.EntityFrameworkCore`
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0;net11.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
20 changes: 16 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
tested) against the EF Core major that ships for it, so the package's dependency group for
that TFM pulls the matching EF Core. The providers are test-only: tests build every model
on each of them (no database needed) to prove the rules are provider-neutral. Pomelo has no
EF Core 10 release yet, so it only covers net8.0/net9.0; Oracle's MySql.EntityFrameworkCore
covers MySQL on all three. Sqlite.Core plus an explicit SQLitePCLRaw bundle rather than
the Sqlite package: the provider loads native SQLite even just to build a model, and the
bundle the Sqlite package pulls in (2.1.6-2.1.11) ships a known-vulnerable SQLite.
EF Core 10 or 11 release yet, so it only covers net8.0/net9.0; Oracle's
MySql.EntityFrameworkCore covers MySQL on all four. Sqlite.Core plus an explicit
SQLitePCLRaw bundle rather than the Sqlite package: the provider loads native SQLite even
just to build a model, and the bundle the Sqlite package pulls in (2.1.6-2.1.11) ships a
known-vulnerable SQLite.
-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.31" />
Expand Down Expand Up @@ -67,4 +68,15 @@
-->
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="10.0.14" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net11.0'">
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="11.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="11.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="11.0.0" />
<!--
Test-only, never packed, so a prerelease is fine here: Npgsql and Oracle's MySQL
provider have no stable EF Core 11 release yet. Move them to stable once they ship.
-->
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="11.0.0-rc.3" />
<PackageVersion Include="MySql.EntityFrameworkCore" Version="11.0.0-preview" />
</ItemGroup>
</Project>
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ A few details:
`NamingScope.Tables | NamingScope.Columns`. `NamingScope.All`, the default, also covers kinds
that later versions add.
- **JSON columns are skipped by the column facet rules.** That covers owned types mapped with
`ToJson()` and JSON complex types on EF Core 10. Their properties aren't columns. Naming still
checks the JSON container column itself. MR006 is the exception: EF Core writes an enum into
`ToJson()` and JSON complex types on EF Core 10 and later. Their properties aren't columns. Naming
still checks the JSON container column itself. MR006 is the exception: EF Core writes an enum into
a JSON document as a number too, unless it's converted, so enums inside JSON are checked. The
integration tests read the stored JSON back from PostgreSQL and SQL Server to confirm it.
- **MR011 fires on every optional relationship you haven't configured**, because
Expand Down Expand Up @@ -343,15 +343,15 @@ a compiled model the test is where the rules are enforced. `dotnet ef dbcontext

## Compatibility

- EF Core 8, 9 and 10. The package has one build per EF Core major (`net8.0`, `net9.0`,
`net10.0`), and CI tests each one.
- EF Core 8, 9, 10 and 11. The package has one build per EF Core major (`net8.0`, `net9.0`,
`net10.0`, `net11.0`), and CI tests each one.
- A recent EF Core patch. Each build requires at least the EF Core patch it was built and
tested against; the package's NuGet page lists it under Dependencies. If restoring fails with
`NU1605` (a package downgrade), update your EF Core packages to that patch or later. Microsoft
only supports the latest patch of each EF Core major anyway.
- Provider-neutral: rules only read EF Core's relational metadata. The test suite builds the same
models on PostgreSQL (Npgsql), SQL Server, SQLite and MySQL (Oracle's `MySql.EntityFrameworkCore`
on EF Core 8-10; Pomelo on EF Core 8-9, since Pomelo has no EF Core 10 release yet) and
on EF Core 8-11; Pomelo on EF Core 8-9, since Pomelo has no EF Core 10 or 11 release yet) and
checks that every provider reports exactly the same violations.
- Provider features that add shadow properties by design are allowed, e.g. SQL Server temporal
tables' period columns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup Label="NuGet metadata">
<PackageId>Hlibz.EntityFrameworkCore.ModelRules</PackageId>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<Authors>Hlib Zinchenko</Authors>
<Description>Declarative rules that check a finished EF Core model against your team's conventions: no shadow properties, snake_case (or other) naming for every table, column, constraint, index and sequence, decimals with explicit precision, strings with max length, C# nullability matching column nullability, enums stored as strings, one schema per DbContext, aggregate roots that refer to each other by key and carry a concurrency token, no cascade deletes across them, query filters on soft-deletable or tenant-owned entities, delete behaviors the database enforces, no redundant indexes, and identifiers within the database's length limit. Rules run whenever the model is built (including dotnet ef migrations add), or from a unit test via ModelRuleVerifier.Verify(dbContext), so they still apply with compiled models. Every violation is reported at once with a rule ID, and entity types or members can be excluded per rule.</Description>
<Copyright>Copyright (c) 2026 Hlib Zinchenko</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,6 @@ private static void AddJsonContainerColumn(
table.DisplayName());
}

private static string PropertyNames(IReadOnlyList<IReadOnlyProperty> properties) =>
private static string PropertyNames(IEnumerable<IReadOnlyPropertyBase> properties) =>
string.Join(", ", properties.Select(property => property.Name));
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,20 @@ [.. columns.Select(_ => false)],
}
}

/// <remarks>
/// <see langword="null"/> when a member isn't a scalar property mapped to <paramref name="table"/>:
/// from EF Core 11 an index can also hold a complex property, which has no single column, so
/// such an index is left out of the comparison rather than guessed at.
/// </remarks>
private static List<string>? Columns(
IReadOnlyList<IReadOnlyProperty> properties,
IEnumerable<IReadOnlyPropertyBase> properties,
StoreObjectIdentifier table)
{
List<string> columns = [];
foreach (IReadOnlyProperty property in properties)
foreach (IReadOnlyPropertyBase member in properties)
{
if (property.GetColumnName(table) is not { } column)
if (member is not IReadOnlyProperty property
|| property.GetColumnName(table) is not { } column)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net10.0'">
<!--
Npgsql 11.0.0-rc.3 pins EF Core to exactly 11.0.0-rc.3, and the net11.0 tests run it on EF
Core 11.0.0, which builds its models fine. Restore reads NoWarn per project, not per target
framework, hence project-wide; this project is never packed. Drop it once Npgsql ships a
stable EF Core 11 release.
-->
<PropertyGroup>
<NoWarn>$(NoWarn);NU1608</NoWarn>
</PropertyGroup>

<!-- Pomelo has no EF Core 10 or 11 release yet: add it back per TFM once one ships. -->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
<!-- Not used directly: pins a patched MySqlConnector, see Directory.Packages.props. -->
<PackageReference Include="MySqlConnector" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,43 @@ public void NoRedundantIndexes_WithEntitySplitting_ComparesIndexesOnEachTable()
+ "Remove it.",
Assert.Single(violations).Message);
}

#if NET11_0_OR_GREATER
// From EF Core 11 an index can hold a complex property (one mapped to a single JSON column),
// and IReadOnlyIndex.Properties became a list of IReadOnlyPropertyBase; 1.0.0 crashed on it.
[Fact]
public void NoRedundantIndexes_WithJsonComplexPropertyInIndex_SkipsThatIndex()
{
IReadOnlyList<ModelRuleViolation> violations = TestDbContext.Validate(
model => model.Entity<Blog>(blog =>
{
blog.ComplexProperty(x => x.Address, address => address.ToJson());
blog.HasIndex(x => x.Name);
blog.HasIndex(x => new { x.Name, x.Address });
blog.HasIndex(x => x.Address);
}),
rules => rules.NoRedundantIndexes());

Assert.Empty(violations);
}

[Fact]
public void NoRedundantIndexes_WithComplexTypePropertyPrefix_ReportsShorterIndex()
{
IReadOnlyList<ModelRuleViolation> violations = TestDbContext.Validate(
model => model.Entity<Blog>(blog =>
{
blog.ComplexProperty(x => x.Address);
blog.HasIndex(x => x.Address.City);
blog.HasIndex(x => new { x.Address.City, x.Name });
}),
rules => rules.NoRedundantIndexes());

Assert.Equal(
"index 'IX_Blog_Address_City' (Address_City) is a leading prefix of index "
+ "'IX_Blog_Address_City_Name' (Address_City, Name), which serves the same lookups. "
+ "Remove it.",
Assert.Single(violations).Message);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,29 @@ public void MaxIdentifierLength_WithNoneScope_ThrowsInsteadOfCheckingNothing()
context,
rules => rules.MaxIdentifierLength(63, NamingScope.None)));
}

#if NET11_0_OR_GREATER
// From EF Core 11 an index can hold a complex property (one mapped to a single JSON column),
// and IReadOnlyIndex.Properties became a list of IReadOnlyPropertyBase; 1.0.0 crashed on it.
[Fact]
public void MaxIdentifierLength_WithJsonComplexPropertyInIndex_ReportsIndexName()
{
string longIndex = "ix_" + new string('i', 70);

IReadOnlyList<ModelRuleViolation> violations = TestDbContext.Validate(
model => model.Entity<Blog>(blog =>
{
blog.ComplexProperty(x => x.Address, address => address.ToJson());
blog.HasIndex(x => new { x.Name, x.Address }).HasDatabaseName(longIndex);
}),
rules => rules.MaxIdentifierLength(63, NamingScope.Indexes));

ModelRuleViolation violation = Assert.Single(violations);
Assert.Equal("Blog.Name, Address", violation.Target);
Assert.StartsWith(
$"index name '{longIndex}' is 73",
violation.Message,
StringComparison.Ordinal);
}
#endif
}
Loading