SonarSource/sonar-dotnet

Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over their Async counterpart

Closed this issue · 4 comments

Description

S6966 should not fire for DbContext.Add as AddAsync is meant for special purposes only:

This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.

Repro steps

public async Task Test()
{
    await using Microsoft.EntityFrameworkCore.DbContext ctx = new(null!);
    ctx.Add(null!); // S6966
}

Expected behavior

No S6966 for DbContext.Add

Actual behavior

S6966 is generated for the given example

Known workarounds

Suppress warning (in code, config, ...)

Related information

SonarAnalyzer.CSharp 9.25.0.90414

Thank you, @CaringDev, for pointing this out. It is indeed a false positive. PR #9272 documents the misbehavior, and we will track this issue in our backlog.

Using version 9.25.1.91650 this is still an issue.

image

I assume this will be fixed in 9.26? Then it should be assigned to that milestone.