-
Notifications
You must be signed in to change notification settings - Fork 17
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
Entity returned from server is left in a Detached state when it references itself #87
Comments
Good catch! Can you provide an example to reproduce this? |
I'm afraid I can hardly justify the time to contrive a working server-client example for a one-liner. I'm happy to jump on a call and show you, it shouldn't take more than 10 minutes. |
No problem; I can try to do it myself on top of our existing test code. I mostly just want you to clarify the scenario here. class Employee {
public int Id { get; set; }
public int ManagerId { get; set; }
public Employee Manager { get; set; }
} Does the error happen for all Employee-Manager relationships, or only when the Manager is the same entity as the employee and |
Yeah it only happens when the entity references itself, so ManagerId == Id. (Interestingly it does not happen when using EF 4.8 on the server, only for EF Core. The code in materialization/shaper.cs finds the existing object correctly in ObjectStateManager on line 188 in EF 4.8; I was not able to debug into EF Core in enough detail to uncover the problem, so I didn't report to them.) |
If it's a bug in EF Core, we are lucky that the fix is so easy in the Breeze client. |
breeze-client/src/mapping-context.ts
Line 308 in 8326490
When an object referencing itself is returned from the EFCore server, it doesn't correctly use $ref (IMO EFCore bug) and the two objects are in the JSON instead of one, e.g.
{ 'id': 1, 'ownerId': 1, 'owner': { 'id': 1, 'ownerId': 1, 'owner': null, ... }, ... }
. The mapping in Breeze results in entityAspect to have state 'Detached' and an empty entityManager. This is because the reference object is processed first (due to recursion in visitAndMerge) and stored. It is then correctly recognized inside _attachEntityCore and returned, but the return value is disregarded, leaving the original entity being mapped detached.Suggested fix: replace the line with
targetEntity = em._attachEntityCore(targetEntity, EntityState.Unchanged, mergeStrategy);
This would be in accord with EntityManager:
breeze-client/src/entity-manager.ts
Line 797 in 8326490
The text was updated successfully, but these errors were encountered: