Skip to content
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

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

Closed
CaringDev opened this issue May 13, 2024 · 4 comments · Fixed by #9318
Assignees
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@CaringDev
Copy link

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

@martin-strecker-sonarsource
Copy link
Contributor

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.

@martin-strecker-sonarsource martin-strecker-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels May 13, 2024
@costin-zaharia-sonarsource
Copy link
Member

Duplicated by #9301

@mary-georgiou-sonarsource mary-georgiou-sonarsource added the Sprint: Hardening Fix FPs/FNs/improvements label May 21, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource moved this from To do to In progress in Best Kanban May 23, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource changed the title Fix S6966 FP: EntityFramework DbContext Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over there Asnyc counterpart May 23, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource changed the title Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over there Asnyc counterpart Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over their Asnyc counterpart May 23, 2024
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban May 23, 2024
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban May 23, 2024
Best Kanban automation moved this from Review approved to Validate Peach May 23, 2024
@grosch-intl
Copy link

Using version 9.25.1.91650 this is still an issue.

image

@cremor
Copy link

cremor commented May 24, 2024

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

@costin-zaharia-sonarsource costin-zaharia-sonarsource added this to the 9.26 milestone May 24, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource moved this from Validate Peach to Done in Best Kanban May 28, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over their Asnyc counterpart Fix S6966 FP: EntityFrameworks DbContext/DBSet Add/AddRange methods are preferred over their Async counterpart May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
6 participants