-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 3.1- Parameter does not follow the model definition used for the entity property #27106
Comments
@shvmgpt116 This should be fixed in a more recent version of EF Core. Please update to EF Core 6.0.1, and report back if the issue still happens. |
@ajcvickers I tried with EF Core 6.0.1, but the issue is reproducible there as well.
|
@smitpatel Looks like parameter isn't getting inferred type mapping when
Query from
Code: public partial class MyTable
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.ConnectionString)
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
public DbSet<MyTable> MyTable { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyTable>(entity =>
{
entity.ToTable("MY_TABLE");
entity.Property(e => e.Id)
.HasColumnName("ID")
.ValueGeneratedNever();
entity.Property(e => e.FirstName)
.HasColumnName("FIRST_NAME")
.HasMaxLength(20)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasColumnName("LAST_NAME")
.HasMaxLength(20)
.IsUnicode(false);
});
}
}
public class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var firstName = "Arthur";
var item = context.MyTable.Where(x => x.FirstName.StartsWith(firstName)).ToList();
}
}
} |
This is happening because of earlier part of |
@ajcvickers If the application makes sure that the parameter cannot be empty string. For Ex:- if (!string.IsNullOrEmpty(firstName))
var item = context.MyTable.Where(x => x.FirstName.StartsWith(firstName)).ToList(); Then, is there any way to avoid generation of condition |
@shvmgpt116 As a workaround, you should be able to use DbFunctions.Like |
Same root cause as #19503. |
@ajcvickers @smitpatel Please could you confirm if you are planning to fix the issue in EFCore 6.0 , 5.0 and 3.1. |
@shvmgpt116 We don't currently plan to patch this issue; the fix is not low-risk. See the planning process for how we determine which issues to patch. |
I have a sample application where I am applying code first migration.
Here is the entity definition.
Here is the table that gets created after migration.
After that I am executing following linq-
Here is the query that gets executed.
Here I see the parameter that is created has incorrect size (4000) since FIRST_NAME can have max length only 20.
Also the parameter is being compared to a Unicode character even though IsUnicode(false) is set for the corresponding entity property.
If I execute following query which does not use 'StartWith()', then I don't see such issues,
Here is the generated sql-
I found following github link which discusses about similar issue.
#4686
I am wandering why is there this mismatch for the parameter for two different but similar linq query.
There could potential performance issue if the non-unicode string type gets compared against unicode parameter or vice-versa.
Include provider and version information
EF Core version: 3.1.22
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.11.2
The text was updated successfully, but these errors were encountered: