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

Some values in queries are not going through value converter #11347

Closed
cincuranet opened this issue Mar 20, 2018 · 1 comment
Closed

Some values in queries are not going through value converter #11347

cincuranet opened this issue Mar 20, 2018 · 1 comment

Comments

@cincuranet
Copy link
Contributor

With the setup below the query db.Set<FooBar>().Where(x => x.FooBarBaz.Contains("test")).Load() is translated to:

SELECT [x].[Id], [x].[FooBarBaz]
FROM [FooBar] AS [x]
WHERE CHARINDEX(N'test', [x].[FooBarBaz]) > 0

The test didn't went through the converter. Similarly db.Set<FooBar>().Where(x => x.FooBarBaz.StartsWith("test")).Load() and db.Set<FooBar>().Where(x => x.FooBarBaz.EndsWith("test")).Load() respectively result in following SQL where the LEN is not not transformed:

SELECT [x].[Id], [x].[FooBarBaz]
FROM [FooBar] AS [x]
WHERE [x].[FooBarBaz] LIKE N'ToStoreExpr' + N'ToStoreExpr' AND (LEFT([x].[FooBarBaz], LEN(N'test')) = N'ToStoreExpr')
SELECT [x].[Id], [x].[FooBarBaz]
FROM [FooBar] AS [x]
WHERE RIGHT([x].[FooBarBaz], LEN(N'test')) = N'ToStoreExpr'

Setup

class FooBar
{
	public int Id { get; set; }
	public string FooBarBaz { get; set; }
}

class MyContext : DbContext
{
	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		base.OnConfiguring(optionsBuilder);

		optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Integrated Security=true;database=test");
	}

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		base.OnModelCreating(modelBuilder);

		modelBuilder.Entity<FooBar>()
			.Property(x => x.FooBarBaz)
			.HasConversion(new DummyConverter());
	}
}

class DummyConverter : ValueConverter<string, string>
{
	public DummyConverter(ConverterMappingHints mappingHints = default)
		: base(ToStoreExpr, FromStoreExpr, mappingHints)
	{ }

	static Expression<Func<string, string>> FromStoreExpr = _ => nameof(FromStoreExpr);
	static Expression<Func<string, string>> ToStoreExpr = _ => nameof(ToStoreExpr);
}

Further technical details

EF Core version: 2.1.0-preview1-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer

@ajcvickers
Copy link
Member

ajcvickers commented Mar 22, 2018

This is a known issue with converters which is due to:

We plan to add a warning in 2.1.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants