DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Query Tags With Entity Framework Core 2.2

Using Query Tags With Entity Framework Core 2.2

A quick tutorial on how to use query tags to make working with queries and their logs easier.

Gunnar Peipman user avatar by
Gunnar Peipman
·
May. 15, 19 · Tutorial
Like (1)
Save
Tweet
Share
9.77K Views

Join the DZone community and get the full member experience.

Join For Free

Entity Framework 2.2 introduces query tags that make it easier to find specific queries from logs and output windows of Visual Studio. When running an application in the development box, it's possible to live without query tags. We can set breakpoints to see SQL code generated from LINQ queries. But how can we find queries from log files in multithreaded or multiuser scenarios?

Finding Queries From Logs Without Query Tags

But what if the site has multiple users at the same time? The following code works but it's problematic.

_logger.LogInformation("Front page: new photos");
 
model.NewPhotos = await _dataContext.Photos
                                .ProjectTo<PhotoListModel>()
                                .ToListAsync();

Don't be surprised if you see something like this in the logs:

Front page: new photos
Front page: new photos
<Some SQL>
<Different SQL>
<Another SQL>
Front page: new photos
<Some more SQL>
<Some SQL>
<Some SQL>

Looking at logs like this probably raises a question: how can we match the information in the message above with SQL queries in the logs?

Applying Query Tags

Query tags make it easy to answer this question, as query descriptions are always generated with a SQL query. Use query tags like the following example shows.

model.NewPhotos = await _dataContext.Photos.TagWith("New photos for frontpage")
                                .ProjectTo<PhotoListModel>()
                                .ToListAsync();

Using the TagWith() extension method you can be sure that the query description is always logged with the query. You can see query tags in action in the following screenshot.

Query Tags

Query Tags

Wrapping Up

If you want to find out what query was generated by Entity Framework Core for the given LINQ query and you want to be able to find queries from logs, then query tags is the way to go. They don't add much overhead to your application and they make it easy to find generated queries from log files. If you are logging queries anyway then query tags are much smaller in size than SQL queries written to logs and therefore you don't have to worry when using them.

Database Entity Framework Framework

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Low-Code Development: The Future of Software Development
  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • 5 Steps for Getting Started in Deep Learning

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: