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

EF Core 2.1: InvalidOperationException while adding or retrieving object graph #12227

Closed
grayver opened this issue Jun 4, 2018 · 6 comments
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Milestone

Comments

@grayver
Copy link

grayver commented Jun 4, 2018

I have a project with complex data model structure. It works fine with EF Core 2.0. But after upgrading to EF Core 2.1 I get errors on adding or retrieving object graph from database:

Exception message:
InvalidOperationException: The foreign key '{QuestTaskId: -2147482647}' set on 'TaskChoice' with the key value '{Id: -2147482647}' matches an entity of type 'QuizTask', however the principal entity type should be assignable to 'HiddenAreaTask'.
Stack trace:
System.InvalidOperationException: The foreign key '{QuestTaskId: -2147482647}' set on 'TaskChoice' with the key value '{Id: -2147482647}' matches an entity of type 'QuizTask', however the principal entity type should be assignable to 'HiddenAreaTask'.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet`1 handledForeignKeys, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node, Boolean force)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
   at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Add(TEntity entity)
   at EfCoreQueryBug.Controllers.ValuesController.Get() in D:\git\efcore21-query-bug\EfCoreQueryBug\Controllers\ValuesController.cs:line 70
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Steps to reproduce

I prepared example project which stably reproduces this error:
https://github.com/grayver/efcore21-query-bug

Further technical details

EF Core version: 2.1.0
Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL, Microsoft.EntityFrameworkCore.InMemory
Operating system: Windows 10 version 1803 (OS Build 17134.48)
IDE: Visual Studio 2017 version 15.7.3

@ajcvickers
Copy link
Member

Notes for triage: I investigated this and it looks like the root cause is:

  • StateManager.GetPrincipal can return the wrong principal if multiple FKs reference the same principal key.
  • Validation added in 2.1 now checks for this instead of using the value, which in this case happened to work.

@ajcvickers ajcvickers self-assigned this Jun 6, 2018
@ajcvickers ajcvickers added this to the 2.2.0 milestone Jun 6, 2018
@eveneveneven
Copy link

@grayver Did you find any workaround for this issue?

@grayver
Copy link
Author

grayver commented Jun 29, 2018

Unfortunately no.. I upgraded all my projects to .NET Core 2.1, except EntityFramework packages. They are still 2.0
Taking in account that this bug was assigned 2.2 milestone, which will be released in the end of year, it is very frustrating..

@ajcvickers ajcvickers removed this from the 2.2.0 milestone Jun 29, 2018
ajcvickers added a commit that referenced this issue Jul 5, 2018
…ships

Fixes #12227

This issues happens when the principal side of multiple relationships is using inheritance and the relationships share and FK property and principal key definition. In this case, the FK value may match instances that are not of the correct type for the given relationship. This would normally be an error and we started throwing for it in 2.1. But it is not an error if the principal is correct for some other relationship sharing the same FK property. So the fix here is to check all the FKs to see if any one can be used. If so, then no problem. If not, then still an error to catch the common case. No matches is also okay, since we don't know whether the principal is loaded or not.

Note that technically, GetPrincipal is broken and should be fixed with new API surface to get all possible principals, but not doing this here because it would be new API in a patch.
@ajcvickers ajcvickers added this to the 2.1.3 milestone Jul 5, 2018
@Eilon
Copy link
Member

Eilon commented Jul 10, 2018

Patch is approved.

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 11, 2018
AndriySvyryd added a commit that referenced this issue Jul 11, 2018
@optiks
Copy link

optiks commented Jul 13, 2018

We're hitting the same issue - is there a preview release that we can try? How far away is the patch? Thanks.

AndriySvyryd added a commit that referenced this issue Jul 13, 2018
Disable GroupBy_Composite_Select_Dto_Sum_Min_Key_flattened_Max_Avg, flakiness seems external to EF

Fixes #12664
AndriySvyryd added a commit that referenced this issue Jul 13, 2018
Disable GroupBy_Composite_Select_Dto_Sum_Min_Key_flattened_Max_Avg, flakiness seems external to EF

Fixes #12664
AndriySvyryd added a commit that referenced this issue Jul 13, 2018
Disable GroupBy_Composite_Select_Dto_Sum_Min_Key_flattened_Max_Avg, flakiness seems external to EF

Fixes #12664
AndriySvyryd added a commit that referenced this issue Jul 14, 2018
Disable GroupBy_Composite_Select_Dto_Sum_Min_Key_flattened_Max_Avg, flakiness seems external to EF

Fixes #12664
@AndriySvyryd
Copy link
Member

@optiks The patch should come out soon. You can try a preview version from https://dotnetfeed.blob.core.windows.net/orchestrated-release-2-1/20180711-01/final/index.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression type-bug
Projects
None yet
Development

No branches or pull requests

6 participants