ef核心中的实用查询标记

What is query tags in EF Core?

EF Core中的查询标签是什么?

It’s introduced in EF Core 2.2. This feature helps correlate LINQ queries in code with generated SQL queries captured in logs. You annotate a LINQ query using the new TagWith() method.

它是在EF Core 2.2中引入的。 此功能有助于将代码中的LINQ查询与日志中捕获的生成SQL查询相关联。 您使用新的TagWith()方法注释LINQ查询。

var publishedBlogPosts = dbContext.BlogPosts
.Where(b => b.PublishedAt != null)
.TagWith("Getting published blog posts")
.ToList();

When EF generates the SQL with TagWith method, it also includes the tag as a comment in the query; as a result, debugging and profiling queries might be easier.

当EF使用TagWith方法生成SQL时,它还会在查询中包含该标签作为注释。 结果,调试和分析查询可能会更容易。

-- Getting published blog postsSELECT [b].[BlogPostId], [b].[Content], [b].[PublishedAt], [b].[Title]
FROM [BlogPosts] AS [b]
WHERE [b].[PublishedAt] IS NOT NULL

如何使其更实用? (How to make it more practical?)

Debugging and profiling are all about having more information. We can easily access member name, line of code and source file with caller information attributes.

调试和性能分析都是关于获得更多信息的。 我们可以使用呼叫者信息属性轻松访问成员名称,代码行和源文件。

Now take a look to my extension method:

现在看看我的扩展方法:

public static class DbContextExtensions
{
    public static IQueryable<T> TagWithSource<T>(this IQueryable<T> queryable,
        string tag = "",
        [CallerMemberName] string methodName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int line = 0)
    {
        return queryable.TagWith(string.IsNullOrEmpty(tag) 
            ? $"{methodName} - {sourceFilePath}:{line}" 
            : $"{tag}{Environment.NewLine}{methodName}  - {sourceFilePath}:{line}");
    }
}

It retrieves method name, file path and line number from the compiler and apply it on any IQueryable<T>.

它从编译器检索方法名称,文件路径和行号,并将其应用于任何IQueryable<T>

Usage Example:

用法示例:

var publishedBlogPosts = dbContext.BlogPosts
.Where(b => b.PublishedAt != null)
.TagWithSource("Getting published blog posts")
.ToList();

The generated SQL will be something like this:

生成SQL将如下所示:

-- Getting published blog posts
-- Main - D:\Projects\Sample1\Program.cs:17SELECT [b].[BlogPostId], [b].[Content], [b].[PublishedAt], [b].[Title]
FROM [BlogPosts] AS [b]
WHERE [b].[PublishedAt] IS NOT NULL

There is some limitation for this practice, for example on situations like inheriting from BaseRepository you’ll get information about the base class, not the derived class.

这种做法有一些限制,例如,在从BaseRepository继承之类的情况下,您将获得有关基类而不是派生类的信息。

结论 (Conclusion)

Query tagging is a nice feature from EF Core, combining it with caller information is excellent for debugging.

查询标记是EF Core的一项不错的功能,将其与调用者信息结合使用非常适合调试。

翻译自: https://itnext.io/practical-query-tagging-in-ef-core-ad0b38fa3436

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值