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

Don´t emit CA1849 when using DbSet.Add and DbSet.AddRange EntityFramework Methods #6858

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,24 @@ private static void GetSymbolAndAddToList(string symbolName, string metadataName

private static ImmutableArray<IMethodSymbol> GetExcludedMethods(WellKnownTypeProvider wellKnownTypeProvider)
{
var entityFrameworkTypeNames = new[]
{
WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext,
WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbSet1
};

var methodsBuilder = ImmutableArray.CreateBuilder<IMethodSymbol>();
if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftEntityFrameworkCoreDbContext, out INamedTypeSymbol? dbContextType))

foreach (var entityFrameworkTypeName in entityFrameworkTypeNames)
{
foreach (var method in dbContextType.GetMembers().OfType<IMethodSymbol>())
if (wellKnownTypeProvider.TryGetOrCreateTypeByMetadataName(entityFrameworkTypeName, out INamedTypeSymbol? entityFrameworkType))
{
if (method.Name is "Add" or "AddRange")
foreach (var method in entityFrameworkType.GetMembers().OfType<IMethodSymbol>())
{
methodsBuilder.Add(method);
if (method.Name is "Add" or "AddRange")
{
methodsBuilder.Add(method);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,24 @@ public async Task RunAsync(DbContext ctx) {
}.RunAsync();
}

[Fact]
public Task DbSetAddRange_NoDiagnostic()
{
return new VerifyCS.Test
{
TestCode = @"
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

class Test {
public async Task RunAsync(DbSet<object> set) {
set.AddRange(1, 2);
}
}",
ReferenceAssemblies = ReferenceAssemblies.Net.Net70.WithPackages(EntityFrameworkPackages)
}.RunAsync();
}

[Fact]
[WorkItem(6684, "https://github.com/dotnet/roslyn-analyzers/issues/6684")]
public Task DbContextFactoryCreateDbContext_Diagnostic()
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/WellKnownTypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal static class WellKnownTypeNames
public const string MicrosoftCodeAnalysisVisualBasicVisualBasicCompilation = "Microsoft.CodeAnalysis.VisualBasic.VisualBasicCompilation";
public const string MicrosoftCodeAnalysisVisualBasicVisualBasicExtensions = "Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions";
public const string MicrosoftEntityFrameworkCoreDbContext = "Microsoft.EntityFrameworkCore.DbContext";
public const string MicrosoftEntityFrameworkCoreDbSet1 = "Microsoft.EntityFrameworkCore.DbSet`1";
public const string MicrosoftEntityFrameworkCoreEntityFrameworkQueryableExtensions = "Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions";
public const string MicrosoftEntityFrameworkCoreRelationalQueryableExtensions = "Microsoft.EntityFrameworkCore.RelationalQueryableExtensions";
public const string MicrosoftExtensionsLoggingILogger = "Microsoft.Extensions.Logging.ILogger";
Expand Down