question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to translate set operation when matching columns on both sides have different store types

See original GitHub issue

Include your code

using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace HelloEFCore
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var testContext = new TestContext();

            var test1 = testContext.Test1.Select(a => new Test
            {
                Id = a.Id,
                Name = "Test1"
            });

            var test2 = testContext.Test2.Select(a => new Test
            {
                Id = a.Id,
                Name = a.Name
            });

            var test = test1.Union(test2).ToList();
            foreach (var item in test)
            {
                Console.WriteLine($"{item.Id}\t{item.Name}");
            }
        }
    }

    public partial class TestContext : DbContext
    {
        public TestContext()
        {
        }

        public TestContext(DbContextOptions<TestContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Test1> Test1 { get; set; }
        
        public virtual DbSet<Test2> Test2 { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("");
            }
        }
    }

    public class Test
    {
        public Guid Id { get; set; }

        public string Name { get; set; }
    }

    public class Test1
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }
    }

    public class Test2
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }
    }
}


Include stack traces

Unhandled exception. System.InvalidOperationException: Unable to translate set operation when matching columns on both sides have different store types.
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplySetOperation(SetOperationType setOperationType, SelectExpression select2, Boolean distinct)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyUnion(SelectExpression source2, Boolean distinct)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateUnion(ShapedQueryExpression source1, ShapedQueryExpression source2)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   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 HelloEFCore.Program.Main(String[] args) in E:\Projects\HelloEFCore\HelloEFCore\Program.cs:line 27
   at HelloEFCore.Program.<Main>(String[] args)

Include provider and version information

EF Core version:5.0.5 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 5.0 Operating system:win 10 IDE: Visual Studio 2019 16.9

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Apr 21, 2021

It may be correct in this case to infer the constant mapping from the column on the other side of the set operation, like how we do with binary operators. But this can be done as part of #19129 when the time comes…

0reactions
smitpatelcommented, Apr 23, 2021

like how we do with binary operators.

Those are orthogonal concepts and it is responsibility of set operations to align the types across column, which is what #19129 tracks specifically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Union with EF Core returns unable to translate set ...
Union with EF Core returns unable to translate set operation since both operands have different 'Include' operations.
Read more >
Unable to translate set operation when matching columns ...
it is not work,and throw out:Unable to translate set operation when matching columns on both sides have different store types.
Read more >
Union with EF Core returns unable to translate set operation ...
... in in the database, doesn't work and it returns "unable to translate set operation when matching columns on both sides have different...
Read more >
Transforming Enumerable into Queryable with Linq
System.InvalidOperationException : Unable to translate set operation when matching columns on both sides have different store types. Solution.
Read more >
C# Unable to translate set operation when matching ...
运行程序时候,错误提示为: Unable to translate set operation when matching columns on both sides have different store types ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found