-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scaffolded DbContext produces CS8618 nullability warning since upgrade to EF Core 7 #29582
Comments
Are you using any kind of IDE (Visual Studio, VS Code, Rider, etc.)? |
Yes, I tried to build the affected project in Rider and in Visual Studio 2022, but I executed all of these commands in a new sandbox project that I created using only the dotnet CLI before I wrote the question. I only opened the My steps were:
Here I opened the ReadOnly.csproj and added
|
@tobiaswatzek can you please share your project so we can see the error happening? If you're rather share it privately, my email is on my github profile. |
I've created a repo with the project https://github.com/tobiaswatzek/efcore-29582. This repo includes the scaffolded context I created as described in my comment above. I had to remove some of the models for confidentiality reasons, but the issue remains reproducible. Additionally, I added a |
So this suddenly bubbles up many warnings that are otherwise not shown !?
|
Note for triage: confirmed that the suppressor doesn't work when the context has a constructor and |
I'll be investigating this soon. |
@ajcvickers @tobiaswatzek can you please try running I also encountered the warning at first, but it disappeared after doing the above: an uninitialized DbSet property on a DbContext doesn't generate a warning/error, while an uninitialized string one does. I suspect this is because there was lingering state from the EF Core 6 analyzer package or similar (see #19597 (comment)). If so, this is simply a temporary transition problem from 6 to 7. Re |
@roji I created a brand new console app. Built it; error. Shutdown the build server and built again; error.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
</ItemGroup>
</Project> // See https://aka.ms/new-console-template for more information
using Microsoft.EntityFrameworkCore;
Console.WriteLine("Hello, World!");
public class Blog
{
public int Id { get; set; }
}
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
public DbSet<Blog> Blogs { get; set; }
} |
Ah, I see now... The suppressor does work without TreatWarningsAsErrors (no warning emitted), but with TreatWarningsAsErrors the error isn't suppressed. @mavasani is it possible to get your input here? In a nutshell, EF Core 7.0 has UninitializedDbSetDiagnosticSuppressor delivered via Microsoft.EntityFrameworkCore.Analyzers, which suppresses the uninitialized non-nullable property warning (CS8618) for properties of type DbSet on types inheriting from DbContext. Everything is working fine when TreatWarningsAsErrors is off (no warning), but with TreatWarningsAsErrors enabled the build fails with an unsuppressed error. I've seen various issues where TreatWarningsAsErrors is mentioned, but AFAIK, CS8618 should be suppressible (according to these criteria). To repro, use @ajcvickers minimal console program above; the Blogs property shouldn't generate CS8618 (but e.g. an uninitialized string property alongside it should). |
@mavasani @sharwell is it possible to get your input on #29582 (comment) (or can you refer me to the right person)? |
Just tried upgrading to EF Core 7 and ran into the same issue... :( |
TBH, I think a better solution than using a diagnostic suppressor would be to mark the |
@Neme12 that's possible, but it's something each and every user of EF Core would have to do on their own DbContext type. The point of the suppressor is to do it once for all DbSet properties on all DbContext types, everywhere. |
@roji I was able to repro this and ran it under the debugger and it seems like an issue in the suppressor. I noted the following:
|
Ah, I looked at the source code for the suppressor and figured out it is looking for an additional location on the diagnostic. The reported diagnostic seems to have an additional location when it is warning, but when bumped to an error with efcore/src/EFCore.Analyzers/UninitializedDbSetDiagnosticSuppressor.cs Lines 33 to 39 in d00e7d5
|
Thanks @mavasani, here's the PR which introduced the additional location on the compiler side: dotnet/roslyn#58096. Should I open an issue for this on the compiler side? |
@roji I am doing it now - I was able to write a compiler unit test showing the bug. |
Blocked on dotnet/roslyn#66037 |
@roji FYI: The core roslyn fix for this got merged into VS2022 17.5 release branch: dotnet/roslyn#66280 |
Great news, thanks @mavasani! |
In case Google leads anyone else here, this issue is still present when using Rider: https://youtrack.jetbrains.com/issue/RIDER-45021/Support-Roslyn-based-suppressors |
Ask a question
I hava a project which generates models and a DbContext using the
dotnet ef dbcontext scaffold
command. This worked great until today when it was upgraded from .NET 6 and EF Core 6 to use .NET 7 and EF Core 7. Now the generated DbContext no longer initializes theDbSet
properties tonull!
(see #26877) and we get CS8618 nullability warnings when compiling. These break our build because we haveTreatWarningsAsErrors
set totrue
for our projects.I tried to add the Microsoft.EntityFrameworkCore.Analyzers package to the project which as I understand should include a new analyzer that suppresses these warnings (see #26795). Unfortunately, this did not change anything.
My workaround for now is to suppress the CS8618 warning for the whole project that includes the generated DbContext.
Is there anything I am missing or is there a bug somewhere in the analyzer or generated code?
Include verbose output
Output when running scaffold:
Output when running
dotnet build
(full paths and unrelated projects removed):Include provider and version information
EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer 7.0.0
Target framework: .NET 7.0
Operating system: Windows 10 Enterprise
IDE: -
The text was updated successfully, but these errors were encountered: