RobinS.Analyzers.EntityFrameworkCore 1.0.0

dotnet add package RobinS.Analyzers.EntityFrameworkCore --version 1.0.0
                    
NuGet\Install-Package RobinS.Analyzers.EntityFrameworkCore -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RobinS.Analyzers.EntityFrameworkCore" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RobinS.Analyzers.EntityFrameworkCore" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RobinS.Analyzers.EntityFrameworkCore">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RobinS.Analyzers.EntityFrameworkCore --version 1.0.0
                    
#r "nuget: RobinS.Analyzers.EntityFrameworkCore, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RobinS.Analyzers.EntityFrameworkCore@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RobinS.Analyzers.EntityFrameworkCore&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RobinS.Analyzers.EntityFrameworkCore&version=1.0.0
                    
Install as a Cake Tool

RobinS.Analyzers.EntityFrameworkCore

A Roslyn analyzer that detects synchronous Entity Framework Core calls in async contexts and suggests async alternatives to prevent thread pool starvation and deadlocks.

Features

  • Detects synchronous EF Core materializers like ToList(), ToArray(), First(), FirstOrDefault(), Single(), SingleOrDefault(), Any(), Count(), All(), Last(), LastOrDefault(), ElementAt(), ElementAtOrDefault(), Min(), Max(), Sum(), Average(), Aggregate(), Contains(), SequenceEqual(), LongCount(), Load()
  • Detects synchronous DbContext.SaveChanges() calls
  • Works with interface-based DbContext patterns
  • Only flags calls on IQueryable<T> types (from EF DbSets) to avoid false positives on in-memory collections
  • Only flags issues in async contexts (methods marked with async keyword or returning Task/ValueTask)
  • Provides clear warning messages suggesting async alternatives

Usage

Add the analyzer to your project:

<PackageReference Include="RobinS.Analyzers.EntityFrameworkCore" Version="1.0.0" PrivateAssets="all" />

Or build from source and reference the output DLL.

Examples

The analyzer will flag these patterns when they appear in async methods:

// In an async method:
async Task GetUsersAsync()
{
    // ❌ Will trigger EFASYNC001
    var users = context.Users.ToList();
    var user = context.Users.First();
    var count = context.Users.Count();
    context.SaveChanges();

    // ✅ Recommended alternatives
    var users = await context.Users.ToListAsync();
    var user = await context.Users.FirstAsync();
    var count = await context.Users.CountAsync();
    await context.SaveChangesAsync();
}

But will ignore these (no false positives):

// ✅ Sync methods in non-async contexts are allowed
void GetUsers()
{
    var users = context.Users.ToList(); // No warning in sync method
}

// ✅ In-memory collections won't trigger the analyzer (even in async methods)
async Task GetListAsync()
{
    var list = new List<int> { 1, 2, 3 };
    var result = list.ToList(); // In-memory collection, not EF
}

// ✅ Works with interfaces and inherited DbContexts
interface IMyContext : IDisposable
{
    DbSet<User> Users { get; }
    int SaveChanges();
}

// Will still detect sync calls through interfaces
async Task GetUsersFromInterfaceAsync()
{
    using IMyContext context = new MyDbContext();
    var users = context.Users.ToList(); // Will be detected
}

Diagnostic Codes (so far)

  • EFASYNC001: Use async EF Core method (Warning)

Project Structure

  • RobinS.Analyzers.EntityFrameworkCore/ - The main analyzer project
    • Core/ - Core analyzer functionality and constants
    • Rules/ - Rule implementations
    • Utils/ - Helper utilities
  • RobinS.Analyzers.EntityFrameworkCore.Tests/ - Unit tests for the analyzer

Building

dotnet build
dotnet test

Roadmap and Contributing

More rules are planned for future releases.

Contributions are welcome! If you have ideas for improvements or new features:

  • Open an issue to discuss your proposal
  • Submit a PR with your changes
  • Ensure tests are included for any new functionality

License

This project is licensed under the MIT License - see the LICENSE file for details.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 1,377 7/10/2025