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

EF Core 1.0 RTM not filtering properly on fields of type char #5975

Closed
iberodev opened this issue Jul 5, 2016 · 5 comments
Closed

EF Core 1.0 RTM not filtering properly on fields of type char #5975

iberodev opened this issue Jul 5, 2016 · 5 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@iberodev
Copy link

iberodev commented Jul 5, 2016

Steps to reproduce

I have created a Github project for this issue
https://github.com/iberodev/EntityFramework5975

The issue

Suppose I have a Port entity

public class Port
{
    public string Code { get; set; }
    public string Name { get; set; }
}

public class PortMap
{
    public PortMap(EntityTypeBuilder<Port> entityBuilder)
    {
        entityBuilder.HasKey(p => p.Code);

        entityBuilder.Property(p => p.Code)
            .HasColumnType("char(5)");

        entityBuilder.Property(p => p.Name)
            .HasMaxLength(65);
    }
}

and the field Code has a char(5) type.

and the following data:
Code Name
AUCNB Canberra
AUMLB Melbourne
AUSYD Sydney
NZAKL Auckland
NZTRG Tauranga

if I use Entity to retrieve rows based on a filter (eg: the word NZ to retrieve only ports whose code starts with NZ), the SQL query Entity generates does not return any results from database.

public IEnumerable<Port> GetPortsFilteredInDatabase(string filter = "NZ")
{
    var query = _context.Ports.Where(p => p.Code.StartsWith(filter));
    return query.ToList(); //this list will be empty
}

public IEnumerable<Port> GetPortsFilteredInMemory(string filter = "NZ")
{
    var allPorts = _context.Ports.ToList();
    var query = allPorts.Where(p => p.Code.StartsWith(filter));
    return query.ToList(); //this list is correct
}

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[@__filter_0='?' (Size = 5)], CommandType='Text', CommandTimeout='30']
SELECT [p].[Code], [p].[Name]
FROM [Ports] AS [p]
WHERE [p].[Code] LIKE @__filter_0 + '%'

Notice the question mark instead the NZ text when generating the SQL statement. Is that normal?

This was working fine in EF Core RC2.

Also notice that if instead CHAR(5), my code column is NVARCHAR(5) everything works well.

Further technical details

EF Core version: 1.0.0
Operating system: Windows 10
Visual Studio version: VS2015 update 3

@iberodev
Copy link
Author

iberodev commented Jul 5, 2016

If my filter, instead NZ, is NZAKL (same lenght as the char(5)) then it returns properly one result. So the question mark in the logs I guess it does not mean what I thought, but still there is an issue here.

@rowanmiller
Copy link
Contributor

The issue is that we are using the datatype from Code column as the data type for the parameter for filter. Because this is char(5), the data gets padded to "NZ " (three trailing spaces).

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jul 8, 2016
@rowanmiller
Copy link
Contributor

You could try this as a workaround... .Where(p => p.Code.StartsWith(filter.Trim()));

@rowanmiller rowanmiller modified the milestones: 1.2.0, 1.1.0-preview1 Oct 6, 2016
ChaosEngine added a commit to ChaosEngine/EntityFramework.Docs that referenced this issue Feb 12, 2017
refact of hashes EF DBcontext optimiations (dotnet/efcore#5975)
ChaosEngine added a commit to ChaosEngine/EntityFramework.Docs that referenced this issue Feb 12, 2017
refact of hashes EF DBcontext optimiations (dotnet/efcore#5975)
Environment.Machine name for docker-compose load balancing
ChaosEngine added a commit to ChaosEngine/EntityFramework.Docs that referenced this issue Feb 12, 2017
refact of hashes EF DBcontext optimiations (dotnet/efcore#5975)
Environment.Machine name for docker-compose load balancing
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@divega divega removed this from the 2.0.0 milestone Jun 12, 2017
@divega divega removed their assignment Jun 12, 2017
@divega
Copy link
Contributor

divega commented Jun 12, 2017

At least on SQL Server, when we create string parameters we should use the variable length flavors of the types e.g. NVARCHAR(x) and VARCHAR(x) and not NCHAR(x) and CHAR(x), otherwise padding will be added.

@divega
Copy link
Contributor

divega commented Jun 12, 2017

Note that this is likely to impact many databases and providers, but I am not sure there is common implementation code where we could fix it. We can add tests to help providers do the same.

@ajcvickers ajcvickers added this to the 2.0.0 milestone Jun 16, 2017
@AndriySvyryd AndriySvyryd removed their assignment Jun 20, 2017
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 20, 2017
ChaosEngine added a commit to ChaosEngine/Dotnet-Playground that referenced this issue Nov 15, 2019
refact of hashes EF DBcontext optimiations (dotnet/efcore#5975)
Environment.Machine name for docker-compose load balancing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants