Dotmim.Sync icon indicating copy to clipboard operation
Dotmim.Sync copied to clipboard

Invalid column name issues

Open spj-uk opened this issue 3 months ago • 1 comments

I have a process where if a database does not exist locally I perform a sync from the server to create the 'template'. It does create it but it looks like the scope info is referring to some old column names. The schema has changed and these are no longer in the database.

I get the following error:.

Invalid column name 'G_Score'. Invalid column name 'G_Remarks'.

at Dotmim.Sync.RemoteOrchestrator.InternalApplyThenGetChangesAsync(ScopeInfoClient cScopeInfoClient, ScopeInfo cScopeInfo, SyncContext context, ClientSyncChanges clientChanges, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.Web.Server.WebServerAgent.ApplyThenGetChangesAsync2(HttpContext httpContext, HttpMessageSendChangesRequest httpMessage, SessionCache sessionCache, Int32 clientBatchSize, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.Web.Server.WebServerAgent.HandleRequestAsync(HttpContext httpContext, Action1 action, CancellationToken cancellationToken, IProgress1 progress)


INNER EXCEPTION

Invalid column name 'G_Score'. Invalid column name 'G_Remarks'.

at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__203_0(Task1 result) at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location --- at Dotmim.Sync.BaseOrchestrator.InternalReadSyncTableChangesAsync(ScopeInfo scopeInfo, SyncContext context, Nullable1 excludintScopeId, SyncTable syncTable, BatchInfo batchInfo, Boolean isNew, Nullable1 lastTimestamp, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.BaseOrchestrator.InternalGetChangesAsync(ScopeInfo scopeInfo, SyncContext context, Boolean isNew, Nullable1 fromLastTimestamp, Nullable1 toNewTimestamp, Nullable1 excludingScopeId, Boolean supportsMultiActiveResultSets, String batchRootDirectory, String batchDirectoryName, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.RemoteOrchestrator.InternalApplyThenGetChangesAsync(ScopeInfoClient cScopeInfoClient, ScopeInfo cScopeInfo, SyncContext context, ClientSyncChanges clientChanges, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.RemoteOrchestrator.InternalApplyThenGetChangesAsync(ScopeInfoClient cScopeInfoClient, ScopeInfo cScopeInfo, SyncContext context, ClientSyncChanges clientChanges, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress) at Dotmim.Sync.RemoteOrchestrator.InternalApplyThenGetChangesAsync(ScopeInfoClient cScopeInfoClient, ScopeInfo cScopeInfo, SyncContext context, ClientSyncChanges clientChanges, DbConnection connection, DbTransaction transaction, CancellationToken cancellationToken, IProgress1 progress)

Here is my code, it is hitting the exeption when calling the line synchronizeasync with reinitialize.

private async Task SynchroniseTempDB()
{
    try
    {
        Uri uri = new Uri(APIURI + "api/CreateTemp" + $"?oID={OID}&name={$"DBTemp"}");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);        

        request.Headers.Clear();
        request.Method = "POST";
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(Username + ":" + Password));

        string postData = "";
        postData = HttpUtility.UrlEncode("name") + "=" + HttpUtility.UrlEncode(Name) + "&"
            + HttpUtility.UrlEncode("type") + "=" + HttpUtility.UrlEncode(Type) + "&"
            + HttpUtility.UrlEncode("orgID") + "=" + HttpUtility.UrlEncode(OID.ToString());

        byte[] data = Encoding.UTF8.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(data, 0, data.Length);
        requestStream.Close();

        HttpWebResponse res = (HttpWebResponse)request.GetResponse();

        WebHeaderCollection header = res.Headers;

        var encoding = ASCIIEncoding.UTF8;
        string responseText = "";

        using (var reader = new System.IO.StreamReader(res.GetResponseStream(), encoding))
        {
            responseText = reader.ReadToEnd().TrimStart('"').TrimEnd('"');
        }

        Uri uriSync = new Uri(APIURI + "Inc" + $"?name={IName}");

        var authenticationBytes = Encoding.UTF8.GetBytes($"{Username}:{Password}");
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authenticationBytes));

        var proxyClientProvider = new Dotmim.Sync.Web.Client.WebRemoteOrchestrator(uriSync.ToString(), client: httpClient);

        string sourcePath = @LocalPath + $"{OrgID}_{IncName}.inc";

        var clientProvider = new SqliteSyncProvider($"Data Source={sourcePath}; Password={DBPassword}");

        proxyClientProvider.HttpClient.Timeout = TimeSpan.FromMinutes(20);

        var progress = new SynchronousProgress<ProgressArgs>(s =>
                   UpdateProgress(s.ProgressPercentage)
               );

        var agent = new SyncAgent(clientProvider, proxyClientProvider, SynchronisationData.Options);

        var serverScope = await proxyClientProvider.GetScopeInfoAsync(); 

        await agent.LocalOrchestrator.DeprovisionAsync();
        await agent.LocalOrchestrator.ProvisionAsync(serverScope);

        var syncResult = await agent.SynchronizeAsync(SyncType.Reinitialize, progress); 

    }
    catch (Exception ex)
    {
        LogError(AppName, AppVersion, GetType().Name, "SynchroniseTempDB", "20", "Error", ex.Message, false);
        SetStatus("Synchronisation failed", Color.Black);
        Close();
    }
}

spj-uk avatar Apr 05 '24 16:04 spj-uk