Celebration Moment
I just wanted to take a moment to express my gratitude for your support in helping my newsletter cross the milestone of 1000 subscribers! This achievement would not have been possible without your help and contribution. Thanks a million :)
Benefits and when to use them
We can use EF Core methods to retrieve/update data from database. By using them we can
Call Store Procedure
Write SQL Queries in LINQ
Combine SQL Query and LINQ
So if you want to call SPs directly via LINQ or you don’t have a way to retrieve/modify data other than SQL Query then these methods are perfect remedy.
Methods
We have following methods
FromSqlInterpolated and FromSql
These methods are safe against SQL Injection attack and can be used to call SP and execute queries.
FromSql is used for parameterized queries that return entity types
FromRawSql
It is helpful in calling the stored procedures and and writing direct SQL in LINQ.
Be careful when using it , if not used properly it can be prey of SQL Injection attack
It is used for parameterized queries that return entity types or primitive types
ExecuteSqlRaw
If we want to perform operation like Insert/Update/Delete then we can use it.
It has async versions as well
Things to keep in mind
The SQL query must return data for all properties of the entity type.
The column names in the result set must match the column names that properties are mapped to.
Whenever you’re ready , there are 2 ways I can help you
Become a Patron and get access to 100+ .NET Questions and Answers at one place , I continuously add 3-4 questions every week.
Download my eBook from Gumroad that contains 30 .NET Tips