EF Core: Unable to materialize entity instance of type 'Resource'. No discriminators matched the discriminator value ''."

2k Views Asked by At

This Stack Overflow question does not solve the problem and is not related: .NET Core: "Unable to materialize entity instance of type 'IdentityUser'. No discriminators matched the discriminator value"

Exception Stack:

 - at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.<PopulateIncludeCollection>g__ProcessCurrentElementRow|60_0[TIncludingEntity,TIncludedEntity](<>c__DisplayClass60_0`2& )\r\n
 - at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.PopulateIncludeCollection[TIncludingEntity,TIncludedEntity](Int32 collectionId, QueryContext queryContext, DbDataReader dbDataReader, SingleQueryResultCoordinator resultCoordinator, Func`3 parentIdentifier, Func`3 outerIdentifier, Func`3 selfIdentifier, IReadOnlyList`1 parentIdentifierValueComparers, IReadOnlyList`1 outerIdentifierValueComparers, IReadOnlyList`1 selfIdentifierValueComparers, Func`5 innerShaper, INavigationBase inverseNavigation, Action`2 fixup, Boolean trackingQuery)\r\n
 - at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()\r\n
at General.Collection.ListExtension.AddRange[T](IList`1 source, IEnumerable`1 newList) in C:\\prj\\IreqNew\\MicroReseau\\General\\Collection\\ListExtension.cs:line 222"

Exception TargetSite:
+        TargetSite    {Void <PopulateIncludeCollection>g__ProcessCurrentElementRow|60_0[TIncludingEntity,TIncludedEntity](<>c__DisplayClass60_0`2 ByRef)}    System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

I'm using Table per type (TPT) instead of the recommended table per hierarchy (TPH). Note: Data is small (for hierarchical objects) and performance is not an issue here.

Please note that I received an exception previously while trying to save data in the application that tells me there was a missing table.

I did an Add-Migration and an Update-Datable, but the problem was still there. Why and what it is? Why do I get this exception?

1

There are 1 best solutions below

0
Eric Ouellet On

I found my problem, but I had a hard time to figure it out.

The hierarchy of my case: I had TPT: Resource (parent) → ResourceStorage → Battery (Leaf).

It was due to a missing Table into a TPT hierachy (Table per Type). The missing table was the leaf (instantiable class: Battery). On a saved instance of the missing leaf class (Battery), EF Core saved parents data (in tables Resource and ResourceStorage), but it threw an exception while trying to save into the missing data table (Battery).

Everything in the database was fine but missing the "Battery" table. When I tried to save a Battery, it crashed, but it left the two other tables with one row in each of it (Resource and Resource Storage). EF Core did not makes a transaction for the save, so EF Core left my database in an invalid state for me. Doing an add-migration and update-database did not solve the problem.

I had to remove both rows in order to be able to make it work.

The hard part is to find the invalid data. The only clue you have is located in the Exception message : "Instance of type 'xxx'" where xxx is the parent table where the problem reside. So you have to make some queries to find the offending entity where there is missing a leaf row (or a missing table, like in my case) for a parent row.