EfHashTagGenerator 0.0.2
dotnet add package EfHashTagGenerator --version 0.0.2
NuGet\Install-Package EfHashTagGenerator -Version 0.0.2
<PackageReference Include="EfHashTagGenerator" Version="0.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="EfHashTagGenerator" Version="0.0.2" />
<PackageReference Include="EfHashTagGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add EfHashTagGenerator --version 0.0.2
#r "nuget: EfHashTagGenerator, 0.0.2"
#addin nuget:?package=EfHashTagGenerator&version=0.0.2
#tool nuget:?package=EfHashTagGenerator&version=0.0.2
EfHashTagGenerator
EfHashTagGenerator is a Roslyn Incremental Source Generator that enhances EF Core LINQ query tracing by generating deterministic and consistent SQL query tags based on the call site.
🛠 Usage
- Reference this generator from your project (as an analyzer NuGet package or project reference).
- In your LINQ queries, simply call:
query.TagWithCallSiteHash();
- The source generator will automatically emit:
query.TagWith("#a1b2c3d4");
The tag #a1b2c3d4
is generated from a stable hash of the call site like:
MyClass.GetUsers:L42
🔧 How It Works
- Scans all invocations of
TagWithCallSiteHash()
in your codebase. - Extracts:
- File path
- Method name
- Line number
- Computes a deterministic hash code.
- Emits a helper class:
internal static class EfHashTagExtensions
{
public static IQueryable<T> TagWithCallSiteHash<T>(
this IQueryable<T> query,
[CallerFilePath] string filePath = null,
[CallerMemberName] string memberName = null,
[CallerLineNumber] int lineNumber = 0)
{
var location = $"{Path.GetFileNameWithoutExtension(filePath)}.{memberName}:L{lineNumber}";
var hashTag = GetHashTagByLocation(location);
return query.TagWith(hashTag);
}
private static string GetHashTagByLocation(string location)
{
switch (location)
{
case "Test0.Main:L38": return "#c9c32584";
// ...
default: return location;
}
}
}
📝 Requirements
- EF Core (uses
.TagWith()
internally)
📂 Output Location
Add the following code to your project file to add the generated code to the project folder to be able to find the source code by hashtag later:
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
And then exclude it from compilation
<ItemGroup>
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>
Useful links:
- Query tagging
- Better Tagging of EF Core Queries with .NET 6
- Automatic tagging with DbCommandInterceptor and StackTrace
- Why is string.GetHashCode() different each time I run my program in .NET Core?
- Slow Sql Server queries tagged with callsite hash
📃 License
Apache 2.0
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.