Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create parameterized queries #24

Closed
VahidN opened this issue Sep 27, 2021 · 4 comments
Closed

Create parameterized queries #24

VahidN opened this issue Sep 27, 2021 · 4 comments
Assignees
Labels
close fixed enhancement New feature or request entity framework Integration with entity-framework

Comments

@VahidN
Copy link

VahidN commented Sep 27, 2021

This library in-lines the field's values in EF-Core queries. For instance it creates queries like this:

   WHERE [m].[Title] = N'action'

For performance reasons (caching the query plan by SQL Server) it's better to create parameterized queries such as:

   WHERE [m].[Title] =  @__p_0
@alirezanet alirezanet self-assigned this Sep 27, 2021
@alirezanet alirezanet added enhancement New feature or request need more information Further information is requested labels Sep 27, 2021
@alirezanet
Copy link
Owner

alirezanet commented Sep 27, 2021

Hi, Does this query translate to a parameterized Sql query using SqlServer provider?

dbContext.Blogs.Where( Param_0 =>  Param_0.Title == "action")

The thing is gridify doesn't control the provider's behavior. if you have a filter like title=action gridify generates the above query so you should see the same results in native LINQ and gridify generated expression,
I will look into this more, but first I need to find out what is causing the provider different behavior between native LINQ and gridify.

@alirezanet
Copy link
Owner

alirezanet commented Sep 29, 2021

I did some research and few tests. the behavior of the Native LINQ and gridify generated expression is the same in SqlServer Provider.

// using SqlServer provider
     [Fact]
      public void ApplyFiltering_GeneratedSqlShouldMatch_SqlServerProvider()
      {
         var actual = _dbContext.Users.ApplyFiltering("name = vahid").ToQueryString();
         var expected = _dbContext.Users.Where(q => q.Name == "vahid").ToQueryString();

         Assert.Equal(expected , actual);
      }
SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = N'vahid'

according to this test, the question is, does even this feature you mentioned exist in Entity Framework ?! if yes, how we can use it because looks like we can not use it even with Native LINQ., did I missed something?

alirezanet added a commit that referenced this issue Sep 29, 2021
@alirezanet
Copy link
Owner

alirezanet commented Sep 29, 2021

Never mind, I found the parameterized way to do this in LINQ.
This test is failing because of additional parameter exist in the final query:

      [Fact]
      public void ApplyFiltering_GeneratedSqlShouldMatch_UsingVariable_SqlServerProvider()
      {
         var name = "vahid";
         var actual = _dbContext.Users.ApplyFiltering("name = vahid").ToQueryString();
         var expected = _dbContext.Users.Where(q => q.Name == name).ToQueryString();

         Assert.Equal(expected , actual);
      }
DECLARE @__name_0 nvarchar(4000) = N'vahid';

SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = @__name_0

I never encountered any best practices in the entity framework that says you should always define at least a variable for your constant values!

alirezanet added a commit that referenced this issue Sep 30, 2021
@alirezanet alirezanet mentioned this issue Sep 30, 2021
@alirezanet alirezanet added close fixed and removed need more information Further information is requested labels Sep 30, 2021
@alirezanet
Copy link
Owner

alirezanet commented Sep 30, 2021

This feature is available in version v2.2.1.
To activate Query optimization for entity framework you need to Enable the EntityFrameworkCompatibilityLayer

  GridifyGlobalConfiguration.EnableEntityFrameworkCompatibilityLayer();

e.g

 _dbContext.Users.ApplyFiltering("name = vahid")

when EntityFrameworkCompatibilityLayer is enable the output is

DECLARE @__Value_0 nvarchar(4000) = N'vahid';

SELECT [u].[Id], [u].[CreateDate], [u].[FkGuid], [u].[Name]
FROM [Users] AS [u]
WHERE [u].[Name] = @__Value_0

@alirezanet alirezanet added the entity framework Integration with entity-framework label Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
close fixed enhancement New feature or request entity framework Integration with entity-framework
Projects
None yet
Development

No branches or pull requests

2 participants