This is a slightly subjective question about a specific situation. The main purpose of this question for me is to remind myself that I need to copy the solution. However, if there is already a solution or an alternative approach, I would like to know.
I am working on a project and I am using Entity Framework 4 to access the database. Database design is something that I do not control. The database was developed many years ago, and, in my opinion, the database design is not suitable for the current purposes of the database. This leads to very complex queries.
This is the first time I use Entity Framework in a project, but I have a lot of experience in development compared to MS SQL Server.
What I did again and again is this:
- I am writing a complex L2E request. The query either slows down or returns incorrect results.
- I am looking at my L2E request and I absolutely do not know how to improve it.
- I run SQL Profiler and grab the SQL that EF generates from my query
- I want to run part of this sql to identify the part of the query that is causing the problems.
- The request comes through sp_executesql with a dozen parameters, because if the parameter is used 3 times in the request, L2E creates 3 parameters and passes all of them the same value. The same goes for each parameter.
- Now I need to extract SQL from sp_executesql, unescape of all escaped apostrophes and replace each parameter in the query with its value
- After that, I can finally run the request parts and indicate the problem.
- I go back to my L2E code, change it to fix the problem I found, and the loop repeats.
Honestly, I'm starting to think that you should not use ORM if you do not have your own database design.
As an aside, the unescaping sql process and parameter substitution is the one I want to automate. The goal is to get a bare, de-parameterized sql that I can run in SSMS.
This is a very simple example of what I see in the profile and what I want to get as a result. My real cases are many times more complicated.
Capture:
exec sp_executesql N'SELECT [Extent1].[ProductName] AS [ProductName] FROM [dbo].[Products] AS [Extent1] INNER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID] WHERE ([Extent1].[UnitPrice] > @p__linq__0) AND ([Extent2].[CategoryName] = @p__linq__1) AND (N''Chang'' <> [Extent1].[ProductName])',N'@p__linq__0 decimal(1,0),@p__linq__1 nvarchar(4000)',@p__linq__0=1,@p__linq__1=N'Beverages'
Desired Result:
SELECT [Extent1].[ProductName] AS [ProductName] FROM [dbo].[Products] AS [Extent1] INNER JOIN [dbo].[Categories] AS [Extent2] ON [Extent1].[CategoryID] = [Extent2].[CategoryID] WHERE ([Extent1].[UnitPrice] > 1) AND ([Extent2].[CategoryName] = N'Beverages') AND (N'Chang' <> [Extent1].[ProductName])
I'm just going to write code to convert what I liked first, for example, to the second, if there is nothing better, I will post the solution here. But maybe this is already done by someone? Or maybe there is a profiler or something else that can give me sql code that I can partially execute in SSMS?