-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
LINQ "Contains" generates SQL with wrong char length #32325
Comments
Minimal repro: await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Persons.Add(new() { ThreeCharacterProperty = "AAA", FiveCharacterProperty = "BBBBB" });
context.SaveChanges();
var queryData = new[] { "AAA;BBBBB", "AAA;CCCCC" };
Console.WriteLine(context.Persons.Count(x => queryData.Contains(x.ThreeCharacterProperty + ";" + x.FiveCharacterProperty)));
public class BlogContext : DbContext
{
public DbSet<Person> Persons { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Person
{
public int Id { get; set; }
[Column(TypeName = "char(3)")]
public string ThreeCharacterProperty { get; set; }
[Column(TypeName = "char(5)")]
public string FiveCharacterProperty { get; set; }
} What's happening here is that our type inference logic for concatenation (and other binary types) simply selects the left type mapping when there are mappings on both sides. This bubbles up the tree, and then gets inferred and applied to the columns coming out of the OPENJSON expression. I'm pretty sure we already have other bugs because of this. The correct fix here would be to improve our type inference logic, so that instead of just taking the left mapping we'd combine the two (opened #32333). We may also be able to do something more narrowly-targeted that would make sense in a patch. |
A temporary workaround that helped me with this issue was to disable EF8's JSON features by adding this to my dbcontext.
|
I have same problem. It will be nvarchar (max) |
It was a little different from the result I imagined, but it works.
it is translated to
This idea may be effective if you need to take temporary action until the modified version comes out. |
File a bug
My program worked correctly with EF7, but when I upgraded to EF8,I encountered this problem. When I use Contains(x => x.Property1 + ";" + x.Property2) in a LINQ query, the generated SQL uses a variable length limited to the length of only Property1, so the Contains() always returns zero results.
This also happens with varchar columns, not just char columns.
Include your code
Full demo attached.
snippet from my attached demo:
Generated SQL:
BugRepro1.zip
Include provider and version information
EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework:.NET 8.0
Operating system: Windows 10 Pro 22H2
IDE: Visual Studio 2022 17.8.0
The text was updated successfully, but these errors were encountered: