From 83e4c382a1dcec84463b079421eff4c0edd24747 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 23 Oct 2023 11:10:20 +0800 Subject: [PATCH 1/2] Add `Raw SQL queries for unmapped types` article. --- .../POST.md | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md diff --git a/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md b/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md new file mode 100644 index 0000000000..7a783ec40d --- /dev/null +++ b/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md @@ -0,0 +1,53 @@ +# New Raw SQL queries for unmapped types feature with EF Core 8.0 + +## Introduction + +I would love to talk to you about the new feature in EF Core 8.0, specifically the `raw SQL queries for unmapped types`. +This feature was recently introduced by Microsoft and is aimed at providing more flexibility and customization in database queries. + +## What is the raw SQL queries for unmapped types feature? + +To give you a better understanding, let's look at a sample repository method with abp framework. +Here is an example of a raw SQL query using the new feature: + +````csharp +public interface IAuthorRepository : IRepository +{ + Task> GetAllAuthorNamesAsync(); +} + +public class AuthorIdWithNames +{ + public Guid Id { get; set; } + + public string Name { get; set; } +} + +public class EfCoreAuthorRepository : EfCoreRepository, IAuthorRepository +{ + public EfCoreAuthorRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public virtual async Task> GetAllAuthorNamesAsync() + { + return await (await GetDbContextAsync()).Database.SqlQuery(@$"SELECT Id, Name FROM Authors").ToListAsync(); + } +} +```` + +In this code, we can see that we are using the `SqlQuery` method to execute a raw SQL query on a custom type, in this case, `AuthorIdWithNames`. This allows us to retrieve data that may not be mapped to any of our entity classes in the context. + +## In summary + +This feature can be particularly useful in scenarios where we need to access data from tables or views that are not directly mapped to our entities. It also provides an alternative to using stored procedures for querying data. + +However, it's important to note that using raw SQL queries can increase the risk of SQL injection attacks. So, it's recommended to use parameterized queries to prevent this. Additionally, this feature may not work with certain database providers, so it's important to check for compatibility before implementing it. + +In conclusion, the raw SQL queries for unmapped types feature in EF Core 8.0 is a great addition for developers looking for more flexibility in database queries. It allows us to work with data that may not be directly mapped to our entities and can be a useful tool in certain scenarios. Just remember to use parameterized queries and check for compatibility before implementing it. + +## References + +- [Raw SQL queries for unmapped types](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#raw-sql-queries-for-unmapped-types) +- [SQL Queries](https://learn.microsoft.com/en-us/ef/core/querying/sql-queries#querying-scalar-(non-entity)-types) From c3fe1345bc7c263e7906ef217648a20848c00179 Mon Sep 17 00:00:00 2001 From: Hamza Albreem <94292623+braim23@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:45:51 +0300 Subject: [PATCH 2/2] Update POST.md --- .../POST.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md b/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md index 7a783ec40d..11d8e67563 100644 --- a/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md +++ b/docs/en/Community-Articles/2023-10-23-NET-8-Feature-raw-sql-queries-for-unmapped-types/POST.md @@ -2,12 +2,12 @@ ## Introduction -I would love to talk to you about the new feature in EF Core 8.0, specifically the `raw SQL queries for unmapped types`. +I would love to talk about the new feature in EF Core 8.0, specifically the `raw SQL queries for unmapped types`. This feature was recently introduced by Microsoft and is aimed at providing more flexibility and customization in database queries. -## What is the raw SQL queries for unmapped types feature? +## What is the raw SQL queries for the unmapped types feature? -To give you a better understanding, let's look at a sample repository method with abp framework. +To give you a better understanding, let's look at a sample repository method with the ABP framework. Here is an example of a raw SQL query using the new feature: ````csharp @@ -37,7 +37,7 @@ public class EfCoreAuthorRepository : EfCoreRepository