Skip to content

Client: Wrong entity type used for DatasyncOperationsQueue when using EF Core dynamic proxiesΒ #211

@david1995

Description

@david1995

Describe the bug

I am using the Datasync client in addition to EF Core dynamic proxies.
Inserting an entity works great, but then the DbContext is disposed (e.g. close application).
Using a new instance of the DbContext, the previously inserted entity is then edited and saved.
This creates a second entry in the DatasyncOperationsQueue and causes the subsequent push to the Datasync server to fail or not include the changes from the edit.

To Reproduce

I have created a repository to reproduce the behaviour: https://github.com/david1995/CommunityToolKit.Datasync-DynamicProxiesRepro.
It generates a database file in the bin/debug folder and follows these steps:

  • Create an instance of an OfflineDbContext-derived class
  • Use it to add a new entity
  • Save changes
  • Dispose context
  • Create a new context (e.g. user restarts application)
  • Get previously inserted entity (which now is a Castle.Proxies proxy type of the entity
  • Edit the inserted entity
  • Save changes
  • DatasyncOperationsQueue now contains two entries for the same entity (one with a Castle.Proxies type)
    • additionally, JsonIgnores are not respected, see Additional Context

A screenshot from the SQLite database after program execution is also included.

I think I have tracked down the root of the issue and am happy to provide a fix.

Expected behavior

DatasyncOperationsQueue should include only one entry per entity with the correct EntityType

What platforms?

  • Server:

    • Same PC as client
    • Datasync version: 8.0.5
  • Client:

    • What platform (Android, iOS, Windows, etc.) versions are you running on: Windows 11 build 22631 (also observable on Windows 10)
    • Version of dotnet being used to compile: 9.0.102
    • .NET Runtime Environment: WinUI 3 (Windows App SDK, unpackaged) and Console App on Windows
    • Datsync Toolkit NuGet versions: 8.0.5
    • GitHub repository containing the code: https://github.com/david1995/CommunityToolKit.Datasync-DynamicProxiesRepro

Screenshots

Image

Additional context

There is a side effect, which in my opinion, is not directly related to the Datasync framework.
Seemingly, the JsonIgnoreAttribute from System.Text.Json is not inherited to derived types.
This can be worked around by using a custom IJsonTypeInfoResolver (called CastleProxyResolver in my reproduction repository - just uncomment the DatasyncSerializer lines in Program.cs).
This might still be an issue with intentionally derived classes, but I have not tested it nor do I currently have this use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ClientImprovements or additions to the client code

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions