juniorEntity Framework
How do you execute raw SQL queries in EF Core?
Updated Apr 28, 2026
Short answer
You use FromSqlRaw() or FromSqlInterpolated() to execute raw SQL that maps back to an entity.
Deep explanation
This is useful for complex queries that LINQ cannot translate or for calling stored procedures. The SQL result must include all columns mapped to the entity, and the results are tracked by the DbContext.
Real-world example
Executing a highly optimized legacy stored procedure that returns a list of Products.
Common mistakes
- Using FromSqlRaw with string concatenation instead of parameters, opening the application to SQL Injection attacks.
Follow-up questions
- Can you use raw SQL to return scalar types (like int)?