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

Entity returned from server is left in a Detached state when it references itself #87

Open
erestor opened this issue Nov 15, 2024 · 5 comments · May be fixed by #88
Open

Entity returned from server is left in a Detached state when it references itself #87

erestor opened this issue Nov 15, 2024 · 5 comments · May be fixed by #88

Comments

@erestor
Copy link

erestor commented Nov 15, 2024

em._attachEntityCore(targetEntity, EntityState.Unchanged, mergeStrategy);

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:

attachedEntity = this._attachEntityCore(entity, esSymbol, msSymbol);

@steveschmitt
Copy link
Member

Good catch! Can you provide an example to reproduce this?

@erestor
Copy link
Author

erestor commented Nov 20, 2024

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.

@steveschmitt
Copy link
Member

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.
If I have an entity class that references the same class, like this:

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 ManagerId == Id ?

@erestor
Copy link
Author

erestor commented Nov 20, 2024

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.)

@steveschmitt
Copy link
Member

If it's a bug in EF Core, we are lucky that the fix is so easy in the Breeze client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants