Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created April 8, 2024 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpayneoregon/6cf9e52075834ba57634884c328486a0 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/6cf9e52075834ba57634884c328486a0 to your computer and use it in GitHub Desktop.
Shows results from EF Core statements

When using raw statements with EF Core avoid writing statements where a value for a parameter is embedded as presented in Bad.cs, instead use the methods shown in Good1.cs and Good2.cs which EF Core will create parameters for.

var statement =
"""
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = 9
""";
using var context = new Context();
var list = context.Customers.FromSqlRaw(statement)
.Include(c => c.Contact)
.ToList();
var param = new SqlParameter("@ContactTypeIdentifier", 9);
string statement =
"""
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = @ContactTypeIdentifier
""";
using var context = new Context();
var list = context.Customers.FromSqlRaw(statement, param)
.Include(c => c.Contact)
.ToList();
const int contactTypeIdentifier = 9;
FormattableString statement =
$"""
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = {contactTypeIdentifier}
""";
using var context = new Context();
var list = context.Customers.FromSqlInterpolated(statement)
.Include(c => c.Contact)
.ToList();
info: 4/8/2024 04:38:35.174 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (21ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [n].[CustomerIdentifier], [n].[City], [n].[CompanyName], [n].[ContactId], [n].[ContactTypeIdentifier], [n].[CountryIdentifier], [n].[Fax], [n].[ModifiedDate], [n].[Phone], [n].[PostalCode], [n].[Region], [n].[Street], [c].[CountryIdentifier], [c].[Name], [c0].[ContactId], [c0].[ContactTypeIdentifier], [c0].[FirstName], [c0].[FullName], [c0].[LastName]
FROM (
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = 9
) AS [n]
LEFT JOIN [Countries] AS [c] ON [n].[CountryIdentifier] = [c].[CountryIdentifier]
LEFT JOIN [Contacts] AS [c0] ON [n].[ContactId] = [c0].[ContactId]
----------------------------------------
info: 4/8/2024 04:38:38.438 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (29ms) [Parameters=[@ContactTypeIdentifier='9'], CommandType='Text', CommandTimeout='30']
SELECT [n].[CustomerIdentifier], [n].[City], [n].[CompanyName], [n].[ContactId], [n].[ContactTypeIdentifier], [n].[CountryIdentifier], [n].[Fax], [n].[ModifiedDate], [n].[Phone], [n].[PostalCode], [n].[Region], [n].[Street], [c].[CountryIdentifier], [c].[Name], [c0].[ContactId], [c0].[ContactTypeIdentifier], [c0].[FirstName], [c0].[FullName], [c0].[LastName]
FROM (
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = @ContactTypeIdentifier
) AS [n]
LEFT JOIN [Countries] AS [c] ON [n].[CountryIdentifier] = [c].[CountryIdentifier]
LEFT JOIN [Contacts] AS [c0] ON [n].[ContactId] = [c0].[ContactId]
----------------------------------------
info: 4/8/2024 04:38:41.376 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (4ms) [Parameters=[p0='9'], CommandType='Text', CommandTimeout='30']
SELECT [n].[CustomerIdentifier], [n].[City], [n].[CompanyName], [n].[ContactId], [n].[ContactTypeIdentifier], [n].[CountryIdentifier], [n].[Fax], [n].[ModifiedDate], [n].[Phone], [n].[PostalCode], [n].[Region], [n].[Street], [c].[CountryIdentifier], [c].[Name], [c0].[ContactId], [c0].[ContactTypeIdentifier], [c0].[FirstName], [c0].[FullName], [c0].[LastName]
FROM (
SELECT *
FROM dbo.Customers
WHERE ContactTypeIdentifier = @p0
) AS [n]
LEFT JOIN [Countries] AS [c] ON [n].[CountryIdentifier] = [c].[CountryIdentifier]
LEFT JOIN [Contacts] AS [c0] ON [n].[ContactId] = [c0].[ContactId]
----------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment