format icon indicating copy to clipboard operation
format copied to clipboard

CS8618 not ignored for scaffolded DbContext when EnforceCodeStyleInBuild is enabled

Open idilov opened this issue 3 months ago • 0 comments

Observation: When EnforceCodeStyleInBuild is enabled, dotnet format analyzers --verify-no-changes starts reporting some CS* violations such as CS8618/CS8600(nullability) and CS8321(x never used) but it does not report other CS* violations such as CS0029/CS0165(severe) and CS0168/CS0219(x never used). Whether this is by design or not is another story.

Problem: When EnforceCodeStyleInBuild is enabled, dotnet format reports CS8618 even for a DbSet inside a DbContext which is a special case and is suppressed by Microsoft.EntityFrameworkCore.Analyzers. dotnet build and Visual Studio do not report that warning but dotnet format does. This breaks my automation scripts.

Tested on .net 7, 8, 9 and 10(rc) - no difference. This is a minimal repro:

Program.cs

using MyDbContext db = new();
db.People.Add(new());

MyDbContext.cs

using Microsoft.EntityFrameworkCore;
class MyDbContext : DbContext { public virtual DbSet<Person> People { get; set; } }
public class Person { }

Repro.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <PropertyGroup>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
  </ItemGroup>
</Project>

Output by dotnet format analyzers --verify-no-changes: warning CS8618: Non-nullable property 'People' must contain a non-null value...

Partial workaround: Suppress CS8618 also in .editorconfig e.g. with [*DbContext.cs] dotnet_diagnostic.CS8618.severity = none However, this workaround does not work with WarningsAsErrors for reasons outside dotnet format.

idilov avatar Oct 02 '25 14:10 idilov