"System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched."

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
VPlaksa
Posts: 1
Joined: Wed 10 Apr 2019 08:43

"System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched."

Post by VPlaksa » Thu 11 Apr 2019 06:07

"System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched." when include optional relationships.

There are error occure on trying to get entity 'A' with conditions:
- include entity 'B' (JOIN);
- value 'BId' = null
- enabled filter on entity 'B' (modelBuilder.Entity<B>().HasQueryFilter).

Code: Select all

System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched.
   at lambda_method(Closure , MaterializationContext )
   at Microsoft.EntityFrameworkCore.Query.EntityLoadInfo.Materialize()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.GetEntity(IKey key, EntityLoadInfo entityLoadInfo, Boolean queryStateManager, Boolean throwOnNullKey)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.BufferedEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.BufferedOffsetEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.CompositeShaper.TypedCompositeShaper`5.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
   at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.MoveNextCore(CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs:line 106
   at System.Linq.AsyncEnumerable.AsyncIterator`1.MoveNext(CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs:line 98
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
   at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable`1 source, TAccumulate seed, Func`3 accumulator, Func`2 resultSelector, CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120
   at ConsoleApp6.Program.Main(String[] args) in C:\Users\EmoBook\source\repos\ConsoleApp6\ConsoleApp6\Program.cs:line 43
Root of trouble is wrong generation 'Microsoft.EntityFrameworkCore.Storage.TypeMaterializationInfo' for properties of entity 'B': looks like 'IsFromLeftOuterJoin' has wrong value.
With enabled filter (Added HasQueryFilter) on properties of entity 'B': TypeMaterializationInfo.IsFromLeftOuterJoin is false
With disabled filter (Removed HasQueryFilter) on properties of entity 'B': TypeMaterializationInfo.IsFromLeftOuterJoin is true.

DbContext, entities

Code: Select all

     public class A
    {
        public long Id { get; set; }

        public long? BId { get; set; }

        public virtual B B { get; set; }
    }
     public class B
    {
        public long Id { get; set; }
    }
     public class C: B
    {
    }
    public class TestDbContext : DbContext
    {
        public virtual DbSet<A> As { get; set; }

        public virtual DbSet<B> Bs { get; set; }

        public virtual DbSet<C> Cs { get; set; }

        public TestDbContext(DbContextOptions options)
            : base(options)
        {  }

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

            modelBuilder.Entity<A>(b =>
            {
                b.HasKey(e => e.Id);
                b.HasOne(r => r.B).WithMany().HasForeignKey(r => r.BId);
            });

            modelBuilder.Entity<B>(b =>
            {

                b.HasKey(r => r.Id);
            });
         
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                entityType.FindProperty("Discriminator")?.SetMaxLength(200);
            }

            modelBuilder.Entity<B>().HasQueryFilter(r=> true);
        }  
    }   
Steps to reproduce
Execute code:

Code: Select all

    public static void Run()
    {
            string connectionString = "Data Source=;User ID=;Password=;License Key=";

            IServiceCollection serviceDescriptors = new ServiceCollection();

            serviceDescriptors.AddLogging();
            serviceDescriptors.AddDbContext<TestDbContext>((_, options) =>
            {
                options.UseOracle(connectionString);
            });

            IServiceProvider sp = serviceDescriptors.BuildServiceProvider();


            using (var db = sp.GetService<TestDbContext>())
            {
                if (db.Database.EnsureCreated())
                {
                    db.Add(new A());
                    db.Add(new A());
                    db.SaveChanges();
                }

                var result = db.As.Include(r => r.B).ToList();

            }
    }
Product versions in use:
Microsoft.EntityFrameworkCore 2.2.3
Devart.Data.Oracle.EFCore (9.6.725)
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: "System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched."

Post by Shalex » Sat 20 Apr 2019 15:08

Thank you for your report. We have reproduced the bug and will notify you when it is fixed.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: "System.InvalidOperationException: Unable to materialize entity of type 'B'. No discriminators were matched."

Post by Shalex » Fri 28 Feb 2020 15:46

Your project works with EF Core 3. Please upgrade to EF Core 3.

Post Reply