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

Unable to save extra properties in offline db which are not exists in remote table #258

Open
taddisateesh opened this issue Feb 5, 2025 · 8 comments
Labels
Client Improvements or additions to the client code
Milestone

Comments

@taddisateesh
Copy link

I've integrated the Community data sync client into our .NET MAUI app, and everything is functioning well. However, I'm encountering an issue with the offline sync table. Specifically, in the attached ConstructionDto file, I've added a new property that should be retained only in the offline table and not synced with the remote table. Is this possible? If so, could you please provide a solution?

FYI: This was working fine with old sdk. Reference link : https://github.com/Azure/azure-mobile-apps

  • Server:

    • Version of dotnet being used to compile? 8.0.11
    • Library versions? - 8.0.6
    • What database are you using? - Azure
    • Where are you running the server? - Azure
    • GitHub repository containing the code (optional, but helps!)
  • Client:

    • What platform (Android, iOS, Windows, etc.) versions are you running on? - Android and iOS
    • Does it happen in an emulator / simulator, or only on a real device? - both
    • Version of dotnet being used to compile? - 8.0.11
    • .NET Runtime Environment (WPF, UWP, WinUI3, MAUI, etc.): MAUI
    • Datsync Toolkit NuGet versions? 8.0.6
    • GitHub repository containing the code (optional, but helps!)

Do let me know if you require any additional details
Regrads,
Sateesh taddi.

@taddisateesh taddisateesh added the Requires Triage This issue has not been checked by the project team. label Feb 5, 2025
@adrianhall adrianhall added Question Further information is requested Client Improvements or additions to the client code and removed Requires Triage This issue has not been checked by the project team. labels Feb 5, 2025
@adrianhall
Copy link
Collaborator

There are two methods that you can use:

  1. Put [JsonIgnore] on the property to remain local.
  2. Create a new table with the local only properties and use [DoNotSynchronize] on the DbSet property in the context.

Of the two, (2) is preferred since it ensures that you can reset the table without destroying the local only data. In production, table resets are not unknown as a fix to missing data that can't be re-created except through a re-synchronization event.

@adrianhall adrianhall added the Awaiting Response Awaiting response from the OP label Feb 5, 2025
@taddisateesh
Copy link
Author

@adrianhall Thanks for your response.

For the first case, the property is simply ignored and is not being saved in the local database.

I will try to implement the second approach. However, with the old SDK (https://github.com/Azure/azure-mobile-apps), it used to save local-only properties in the local database along with remote properties without requiring a separate table

@adrianhall
Copy link
Collaborator

adrianhall commented Feb 6, 2025

For the first case, - I think there is something else going on here. JsonIgnore does not affect what is happening via EF Core. It only affects serialization. Thus, it should not be ignored by EF Core. I've got explicit tests for this case, so there is something else happening with your code.

@taddisateesh
Copy link
Author

taddisateesh commented Feb 10, 2025

Hey @adrianhall,

JsonIgnore works fine until we explicitly call PushAsync. However, the marked JsonIgnore property values reset when calling PullAsync after PushAsync

Here is sample code

  public async Task SaveItemAsync(T item, CancellationToken token = default(CancellationToken))
        {
            try
            {
                using (var dbContext = new AppDbContext(dbContextOptionsBuilder.Options))
                {
                    dbContext.TokenRequester = TokenRequester;
                    await ClearExistingQueue(dbContext, typeof(T));
                    if (item == null)
                    {
                        throw new ArgumentException(nameof(item));
                    }
                    ListAction action = (item.Id == null) ? ListAction.Add : ListAction.Update;
                    if (item.Id == null)
                    {
                        item.Id = Guid.NewGuid().ToString("N");
                        await dbContext.Set<T>().AddAsync(item, token);
                    }
                    else
                    {
                        dbContext.Set<T>().Update(item);
                    }
                    await dbContext.SaveChangesAsync();
                    await dbContext.Set<T>().PushAsync();
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            //ItemsUpdated?.Invoke(this, new RemoteServiceEventArgs<T>(action, item));
        }

@taddisateesh
Copy link
Author

@adrianhall and one more thing CommunityToolkit.Datasync.Client ignores nested models and returns null values. Where as it used work Microsoft.Datasync.Client (https://github.com/Azure/azure-mobile-apps).

@adrianhall
Copy link
Collaborator

Microsoft.Datasync.Client never supported nested models. They worked by accident. Because of the move to EF Core for database access (instead of a direct SQlite system), we're explicitly not supporting nested models. The lack of support has been in the documentation for as long as I was managing Azure Mobile Apps (going on a decade now).

I'll work up a design for properly ignoring model properties.

@adrianhall adrianhall removed Question Further information is requested Awaiting Response Awaiting response from the OP labels Feb 10, 2025
@adrianhall adrianhall modified the milestones: 9.0.0, 9.0.1 Feb 10, 2025
@taddisateesh
Copy link
Author

Hey @adrianhall,

JsonIgnore works fine until we explicitly call PushAsync. However, the marked JsonIgnore property values reset when calling PullAsync after PushAsync

Here is sample code

public async Task SaveItemAsync(T item, CancellationToken token = default(CancellationToken))
{
try
{
using (var dbContext = new AppDbContext(dbContextOptionsBuilder.Options))
{
dbContext.TokenRequester = TokenRequester;
await ClearExistingQueue(dbContext, typeof(T));
if (item == null)
{
throw new ArgumentException(nameof(item));
}
ListAction action = (item.Id == null) ? ListAction.Add : ListAction.Update;
if (item.Id == null)
{
item.Id = Guid.NewGuid().ToString("N");
await dbContext.Set().AddAsync(item, token);
}
else
{
dbContext.Set().Update(item);
}
await dbContext.SaveChangesAsync();
await dbContext.Set().PushAsync();
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
//ItemsUpdated?.Invoke(this, new RemoteServiceEventArgs(action, item));
}

@adrianhall Any update on this ?

@adrianhall
Copy link
Collaborator

I’ve validated the request, and tagged it into a release. There will be no update until that release is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client Improvements or additions to the client code
Projects
None yet
Development

No branches or pull requests

2 participants