Search code examples
c#nuget

DotMin.Sync when trying to filter it does not give an error but ignores the filter


I'm using a very interesting package to sync between SQL Server.

As it's something simple, "Download Only", it behaves well and quickly.

Now I need to create a filter - something like

FROM Table_Name 
WHERE tenantID = @myVariable

I'm following the documentation here: https://dotmimsync.readthedocs.io/Filters.html

Apparently the logic is simple and perfect, but I can't run the filter, it syncs but doesn't filter

I have several tables and I want to create a filter, where the tenantID column (FK) is equal to my GUID variable value.

For example:

var setup = new SyncSetup("dbo.Tennants", "dbo.Devices", "dbo.Audios", et cetera...)

setup.Tables["dbo.Tennants"].SyncDirection = SyncDirection.DownloadOnly;
setup.Tables["dbo.Devices"].SyncDirection = SyncDirection.DownloadOnly;
setup.Tables["dbo.Audios"].SyncDirection = SyncDirection.DownloadOnly;

Here the filters start:

var filterTenants = new SetupFilter("Tenants");
filterTenants.AddParameter("TenantId", DbType.Guid); 
filterTenants.AddWhere("TenantId", "Tenants", "TenantId");
setup.Filters.Add(filterTenants);

var filterDevices = new SetupFilter("Devices");
filterDevices.AddParameter("TenantId", DbType.Guid);
filterDevices.AddWhere("TenantId", "Devices", "TenantId");
setup.Filters.Add(filterDevices);

var filterAudios = new SetupFilter("Audios");
filterAudios.AddParameter("TenantId", DbType.Guid);
filterAudios.AddWhere("TenantId", "Audios", "TenantId");
setup.Filters.Add(filterAudios);

I've already tried sending the DbType.Guid type as the TableName ("TenantId", "Audios");

Here I define the created parameter called TenantID for my C# variable _myTenantId

var parameters = new SyncParameters
{
    { "TenantId", _myTenantId }
};

and at the end I execute:

var result = await agent.SynchronizeAsync(setup, parameters, progress);

Of course the code is much larger, error handling, etc., but I don't believe that this influences it.

It synchronizes all TenantIDs and not the one in my variable _myTenantId.

I made a working example to test this problem

  • Create 2 SQL Server databases (Master and Local)
  • Enable Change Tracking Create seed by tenantId create seed by random data
  • Run the Sync database by dotmin creating the filter by tenant It does the sync but with all the data (Random and by tenant) I wanted the local database to have only by TenantId == appSettings.jon

https://github.com/dorathoto/POC_DotMim.Sync.Sample


Solution

  • I discovered that you can't send the schema name along with it. Even though it's stated in the documentation.

    instead of:

    setup.Tables["dbo.Tennants"].SyncDirection = SyncDirection.DownloadOnly;
    

    the correct one:

    setup.Tables["Tennants"].SyncDirection = SyncDirection.DownloadOnly;
    

    Subtle, but the error didn't show this, I discovered it through the author's own answer.

    https://github.com/Mimetis/Dotmim.Sync/issues/1256