-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…EntityFramework #7286 (#7332) Co-authored-by: Sebastien Marichal <[email protected]>
- Loading branch information
1 parent
37ee147
commit d5416d1
Showing
8 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/InsteadOfAny.EntityFramework.Core.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Imports System.Collections.Generic | ||
Imports System.Linq | ||
Imports Microsoft.EntityFrameworkCore | ||
|
||
Public Class EntityFrameworkTestcases | ||
Public Class MyEntity2 | ||
Public Property Id As Integer | ||
End Class | ||
|
||
Public Class MyDbContext | ||
Inherits DbContext | ||
|
||
Public Property MyEntities As DbSet(Of MyEntity2) | ||
|
||
Public Sub GetEntities(ByVal dbContext As MyDbContext, ByVal ids As List(Of Integer)) | ||
Dim __ As IQueryable(Of MyEntity2) = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) e.Id = i)) ' Compliant | ||
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) e.Equals(i))) ' Compliant | ||
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) e.Id > i)) ' Compliant | ||
__ = dbContext.MyEntities.Where(Function(e) ids.Any(Function(i) TypeOf e.Id Is i)) ' Error [BC30002] | ||
End Sub | ||
End Class | ||
End Class |
40 changes: 40 additions & 0 deletions
40
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/InsteadOfAny.EntityFramework.Framework.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Remoting.Contexts; | ||
using System.Threading.Tasks; | ||
using System.Data.Entity; | ||
|
||
// https://github.com/SonarSource/sonar-dotnet/issues/7286 | ||
public class EntityFrameworkReproGH7286 | ||
{ | ||
public class MyEntity | ||
{ | ||
public int Id { get; set; } | ||
} | ||
|
||
public class FirstContext : DbContext | ||
{ | ||
public DbSet<MyEntity> MyEntities { get; set; } | ||
} | ||
|
||
public class SecondContext : DbContext | ||
{ | ||
public DbSet<MyEntity> SecondEntities { get; set; } | ||
} | ||
|
||
|
||
public void GetEntities(FirstContext dbContext, List<int> ids) | ||
{ | ||
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id == i)); // Compliant | ||
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Equals(i))); // Compliant | ||
_ = dbContext.MyEntities.Where(e => ids.Any(i => e.Id > i)); // Compliant | ||
} | ||
|
||
public async Task GetEntitiesAsync(SecondContext secondContext, List<int> ids) | ||
{ | ||
_ = await secondContext | ||
.SecondEntities | ||
.Where(e => ids.Any(i => e.Id == i)) | ||
.ToListAsync(); | ||
} | ||
} |