Skip to content

Query on field non unicode string should not append N #9582

Description

@fchiumeo

I have setting all string use non unicode varchar and select append N in generated SQL

Steps to reproduce

DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var property in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetProperties().Where(p => p.ClrType == typeof(string))))
            {
                property.IsUnicode(false);
            }
        }
 public class TipoServicioConfig : IEntityTypeConfiguration<TipoServicio>
    {
        public void Configure(EntityTypeBuilder<TipoServicio> builder)
        {
            builder.HasKey(ts => ts.Id);

            builder.Property(ts => ts.Id).IsRequired();
            builder.Property(ts => ts.Nombre).IsRequired().HasMaxLength(20);
            
           //it does not work either
          //builder.Property(ts => ts.Nombre).IsRequired().HasMaxLength(20).HasColumnType("varchar(20)");
        }
    }
   public class TipoServicio
    {
 
        public int Id{ get; set; }
        public string Nombre { get; set; }

    }
var query = DbContext.Set<TipoServicio>().Where(xx => xx.Nombre.Contains("lla")).ToList();
      SELECT [xx].[Id], [xx].[Nombre]
      FROM [TipoServicio] AS [xx]
      WHERE CHARINDEX(N'lla', [xx].[Nombre]) > 0

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 (15.3)

Activity

  1. smitpatel commented on Aug 25, 2017

    @smitpatel
    Contributor

    See #4978 (comment)

    Duplicate of #4978

  2. smitpatel commented on Aug 25, 2017

    @smitpatel
    Contributor

    Keeping this open to discuss if we want to add AsUnicode AsNonUnicode API.

  3. fchiumeo commented on Aug 25, 2017

    @fchiumeo
    Author

    please add AsNonUnicode Api or mapping correctly

  4. added this to the 2.1.0 milestone on Aug 30, 2017
  5. ajcvickers commented on Aug 30, 2017

    @ajcvickers
    Contributor

    Triage: try to fix just the Contains issue for now.

  6. added a commit that references this issue on Sep 22, 2017
    a309d61
  7. modified the milestones: , 2.1.0 on Jan 17, 2018
  8. 16 remaining items

  9. added this to the Backlog milestone on Jun 28, 2019
  10. self-assigned this
    on Sep 4, 2019
  11. modified the milestones: Backlog, on Sep 4, 2019
  12. added a commit that references this issue on Sep 9, 2019
  13. added a commit that references this issue on Sep 9, 2019
  14. modified the milestones: , 3.0.0 on Sep 9, 2019
  15. justintubbs commented on May 27, 2020

    @justintubbs

    For those using 2.2 (or pre 3.0) one way to solve this for Oracle is to use a static string value (which will get converted to a parameter) and will no longer have the N prefix in front:

    Linq: (.ComorbidDetail is a nullable column of varchar(100) in Oracle table)

    string staticValue = "0x00D7"; // Whatever you want here
    var query = from c in _dbContext.MemComorbids.AsNoTracking()        
    	join cc in _dbContext.ComorbidCrosswalks
    	on new
    	{
    		c.ComorbidCode,
    		ComorbidDetail = c.ComorbidDetail == null ? staticValue : c.ComorbidDetail
    	} equals new
    	{
    		cc.ComorbidCode,
    		ComorbidDetail = cc.ComorbidDetail == null ? staticValue : cc.ComorbidDetail
    	} into c_cc
    	from crosswalk in c_cc.DefaultIfEmpty()
    	select c;
    

    Oracle SQL Query:

    SELECT *
    FROM "SCHEMA"."PATIENT_COMORBIDS" "c0"
    LEFT JOIN "SCHEMA"."COMORBID_CROSSWALK" "cc0" ON ("c0"."CODE" = "cc0"."CMBD_CODE") AND (CASE
        WHEN "c0"."DETAIL" IS NULL
        THEN :staticValue_0 ELSE "c0"."DETAIL"
    END = CASE
        WHEN "cc0"."CMBD_DETAIL" IS NULL
        THEN :staticValue_0 ELSE "cc0"."CMBD_DETAIL"
    END)
    
  16. removed their assignment
    on Sep 1, 2024
  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions