{"id":23763884,"url":"https://github.com/tomashubelbauer/ef-sql","last_synced_at":"2026-07-03T23:33:23.283Z","repository":{"id":107986061,"uuid":"171253540","full_name":"TomasHubelbauer/ef-sql","owner":"TomasHubelbauer","description":"Printing the SQL generated by EF Core","archived":false,"fork":false,"pushed_at":"2022-04-14T20:12:58.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-25T22:26:59.934Z","etag":null,"topics":["ef","ef-core","entity-framework","entity-framework-core","orm","sql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomasHubelbauer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-02-18T09:26:45.000Z","updated_at":"2022-04-17T09:54:45.000Z","dependencies_parsed_at":"2024-06-02T17:51:12.368Z","dependency_job_id":null,"html_url":"https://github.com/TomasHubelbauer/ef-sql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TomasHubelbauer/ef-sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomasHubelbauer","download_url":"https://codeload.github.com/TomasHubelbauer/ef-sql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasHubelbauer%2Fef-sql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35105483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-03T02:00:05.635Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ef","ef-core","entity-framework","entity-framework-core","orm","sql"],"created_at":"2024-12-31T22:13:47.113Z","updated_at":"2026-07-03T23:33:23.278Z","avatar_url":"https://github.com/TomasHubelbauer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EF Core Query SQL\r\n\r\nThis is a repository dedicated to testing out a reflection-based snippet for printing SQL generated from\r\na EF Core query LINQ expression.\r\n\r\nThe snippet code can be found in multiple places with comments worth following for updated versions:\r\n\r\n- [Stack Overflow question](https://stackoverflow.com/q/37527783/2715716)\r\n- [GitHub gist](https://gist.github.com/rionmonster/2c59f449e67edf8cd6164e9fe66c545a)\r\n- [Blog post 1](http://rion.io/2016/10/19/accessing-entity-framework-core-queries-behind-the-scenes-in-asp-net-core/)\r\n- [Blog post 2](https://weblogs.asp.net/ricardoperes/implementing-missing-features-in-entity-framework-core-part-5-getting-the-sql-for-a-query)\r\n\r\nLet's create the test application and include the EF code NuGet packages:\r\n\r\n```powershell\r\ndotnet new console\r\ndotnet add package Microsoft.EntityFrameworkCore\r\ndotnet add package Microsoft.EntityFrameworkCore.SqlServer\r\n```\r\n\r\nFor the classes see, `AppDbContext`, `User`, `Car` and `Trip` in code.\r\n\r\n```csharp\r\nConsole.WriteLine(appDbContext.Users.Include(u =\u003e u.Car).Where(u =\u003e u.Car != null).ToSql());\r\n```\r\n\r\n```sql\r\nSELECT [u].[Id], [u].[CarId], [u].[Name], [u.Car].[Id], [u.Car].[Make], [u.Car].[Model]\r\nFROM [Users] AS [u]\r\nINNER JOIN [Cars] AS [u.Car] ON [u].[CarId] = [u.Car].[Id]\r\nWHERE [u].[CarId] IS NOT NULL\r\n```\r\n\r\n## To-Do\r\n\r\n### Turn this into a hosted app (Azure Function?) and offer it as a tool for the dev community\r\n\r\n### https://github.com/aspnet/EntityFrameworkCore/wiki/Getting-and-Building-the-Code and debug passing a `First` expression\r\n\r\n### Mention the detailed logging option which shows the executed SQL statements\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomashubelbauer%2Fef-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomashubelbauer%2Fef-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomashubelbauer%2Fef-sql/lists"}