Raw SQL Queries with EF Core Every Developer Should Know About | HOW TO - Code Samples

Raw SQL Queries with EF Core Every Developer Should Know About | HOW TO - Code Samples

STARTUP HAKK

54 года назад

128 Просмотров

https://StartupHakk.com/?v=t4zhp3hWC8s

#coding #codingbootcamp #softwaredeveloper #CodeYourFuture

GitHub Repo: https://github.com/slthomason/StartupHakkSamples/tree/main/119_SQL_Queries_with_EF_Core

EF Core is getting many new and exciting features in the upcoming version.

EF7 introduced support for returning scalar types using SQL queries.

And now we’re getting support for querying unmapped types with raw SQL queries in EF8.

This is exactly what Dapper offers out of the box, and it’s good to see EF Core catching up.

EF Core And SQL Queries
EF7 added support for raw SQL queries returning scalar types. EF8 is taking this a step further with raw SQL queries that can return any mappable type, without having to include it in the EF model.

You can query unmapped types with the SqlQuery and SqlQueryRaw methods.

The SqlQuery method uses string interpolation to parameterize the query, protecting against SQL injection attacks.

Composing SQL Queries With LINQ
An interesting thing about SqlQuery is that it returns IQueryable, which can be further composed with LINQ.

SQL Queries For Data Modifications
If you want to modify data in the database with SQL, you will typically write a query that doesn’t return a result.

The SQL query can be an UPDATE or DELETE statement, or even a stored procedure call.

You can use the ExecuteSql method to execute this type of query with EF Core.


EF7 introduced support for raw SQL queries returning scalar values.

EF8 will add support for raw SQL queries returning unmapped types with SqlQuery and SqlQueryRaw.

I like the direction that EF is going, introducing more flexibility for querying the database.

The performance isn’t as good as Dapper, unfortunately. But it’s close enough that network costs will play the bigger factor.

Lots of Great How Tos and Code Samples! Make sure to like and subscribe!

Тэги:

#software_development #software_engineering #software_developer #software_engineer #custom_software_development #software_myths #azure #azure_engineer #azure_development #Microsoft_azure #c# #dotnet #dotnet_coding #dotnet_coding_bootcamp #html #javascript #LearnToCode #Lean_to_Code #coding #coding_bootcamp #become_software_developer #azure_devops #.net_core #asp.net_core #asp.net #How_to #Code_Samples #Code_Sample #Coding_tutorials #Coding_Tutorial #entity_framework #sql
Ссылки и html тэги не поддерживаются


Комментарии: