Repository navigation
Open
Labels
Milestone
Description
var foo = "12345";
var results = context.Set<Product>()
.Where(c => new List<string?> { "ABCDE", foo }.Where(e => e != null)
.Contains(c.Foo)).ToList();Unhandled exception. System.InvalidOperationException: Conflicting type mappings were inferred for column 'value'.
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.TryGetInferredTypeMapping(TableExpressionBase table, String columnName, RelationalTypeMapping& inferredTypeMapping)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.ProjectionExpression.VisitChildren(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.<VisitChildren>g__VisitList|124_0[T](List`1 list, Boolean inPlace, Boolean& changed, <>c__DisplayClass124_0&)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.VisitChildren(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.InExpression.VisitChildren(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.VisitChildren(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.SqlServerInferredTypeMappingApplier.VisitExtension(Expression expression)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryableMethodTranslatingExpressionVisitor.ApplyInferredTypeMappings(Expression expression, IReadOnlyDictionary`2 inferredTypeMappings)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<Main>$(String[] args) in C:\local\code\AllTogetherNow\Daily\Daily.cs:line 26
at Program.<Main>(String[] args)
using (var context = new SomeDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
}
using (var context = new SomeDbContext())
{
var foo = "12345";
var results = context.Set<Product>()
.Where(c => new List<string?> { "ABCDE", foo }.Where(e => e != null)
.Contains(c.Foo)).ToList();
}
public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>();
}
}
public class Product
{
public int Id { get; set; }
[MaxLength(5)]
public string? Foo { get; set; }
}Reacted by Virma and Seungjae.Park
Activity
ajcvickers commentedon Sep 17, 2023
Note: not a regression from 7.0.
[-]Contains on inline enumerable against column with facet throws[/-][+]Collection type inference fails with conflicting mappings in some cases when a non-default mapping is used[/+]roji commentedon Oct 8, 2023
Here's what's happening here:
This translates to:
nvarchar(5)).nvarchar(max)); this is because when we create the binary expression (!= NULL), since there's no type mapping on either side, SqlExpressionFactory.ApplyTypeMappingOnSqlBinary simply looks up the default type mapping for the left side's CLR type.More generally, this is a result of primitive collections making type inference more complicated and deferred. We should think about how to improve this in 9.0, possibly by not assuming the default type mapping based on the CLR type.
frankbuckley commentedon Jan 1, 2024
I think I may be hitting this - more specifically, when I include this filter:
I get:
If I remove the filter, there is no exception:
(Using EF 8.0 and .NET 8.0.0)
mattpugsley commentedon Mar 5, 2024
Is there a workaround, such as providing explicit ef typing/configuration to a non-model object like a string array? I like to use varchar because it's half the space of nvarchar, and I commonly compare strings with string arrays. I generally exclude nulls with != null before comparing with Contains to ensure that a null value in the sql record doesn't count as being contained by the array.
I can provide an anonymized query if that would help, but I think it's the same issue that has already been identified.
Error message:
System.InvalidOperationException: Conflicting type mappings were inferred for column 'value'. at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.RelationalInferredTypeMappingApplier.TryGetInferredTypeMapping(TableExpressionBase table, String columnName, RelationalTypeMapping& inferredTypeMapping)I recently updated from .net 7 and EF 7 to .net 8 and EF8. I did not encounter the exception before, and if I keep .net 8 but revert to EF 7.0.16, the exception goes away.
roji commentedon Mar 5, 2024
@mattpugsley I'd have to think about it, but unfortunately I don't think there's a workaround... I intend to revisit this and hopefully fix it for 9.0 though.
frankbuckley commentedon Mar 5, 2024
We haven't found one for our examples (which seem to always be filters on
ThenInclude()with a.Contains()).mattpugsley commentedon Mar 25, 2024
I haven't tested with a filtered includes, but the following workaround is successful for me in a where clause:
Instead of trying to make the array look like a varchar array, temporarily make the field look like an nvarchar field by using Convert.ToString(x). Per https://stackoverflow.com/questions/72758236/ef-core-function-to-pass-as-in-cast-value-as-varchar1024, this will tell sql to cast the value as nvarchar(max) (i.e. as part of the generated sql query). Although this might be an issue for projected data (i.e. I would prefer to not cast returned data), this is 'just' filtering, and so I don't have an issue using this cast.
I continue to hope that EF 9 can be made to work with string array Contains against varchar fields (as in EF 7) without this workaround, but until/unless that is possible, I think that this workaround will work for me.
var myArray = new[] { "A", "B" }; myContext.myDbSet.Where(x => myArray.Contains(Convert.ToString(x.MyVarcharField))).ToArray();roji commentedon Mar 25, 2024
Thanks for that info @mattpugsley. This is definitely something we intend to improve for 9.
ta1H3n commentedon Feb 4, 2025
Just noting that this is still an issue in 9.
The workaround to wrap the property in
Convert.ToStringthat @mattpugsley suggested still works on my endAdd test override for Contains_with_local_enumerable_inline_closure_mix
Add test override for Contains_with_local_enumerable_inline_closure_mix
innerbeing a string collection npgsql/efcore.pg#3787 as a duplicate of this issue