Skip to content

CS8618 not ignored for scaffolded DbContext when EnforceCodeStyleInBuild is enabled #2266

@idilov

Description

@idilov

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions