Search code examples
azurewindows-store-appsazure-sql-databaseazure-mobile-services

After doing PullAsync() on a table, I am unable to update the table data - Offline data sync in .Net Mobile service


We are developing a windows store 8.1 app, In that we implemented Offline data sync using Azure mobile Service(.net backend).

We are using Mobile service with On-Premise SQL Server with existing database using Code first migrations.

We have a USER table which stores user emailId and offlinePin in the database.

We also implemented AAD single sign on in the app. For all active directory users those who are going to use the app, we added their email ids to the User table with out offlinePin value.

In client app, We are calling the following line based on the user who log in to the app using AAD single sign on.

Declaration:

private IMobileServiceSyncTable<Users> usertable=  App.MobileService.GetSyncTable<Users>();

await usertable.PullAsync("SyncLoggedInUserInfo", usertable.Where(user => user.Email == App.UserEmail));

Now for the User who log in based on his/her mail Id we are pulling their information from USER table using above line.

If the logged in user don't have an offline pin then the app will prompt the user to create one and save that into the local SQLite USER table.

For updating the user offlinePin we are calling the following lines

var userInfo = await usertable.Where(x => x.Email == App.UserEmail).ToListAsync();
                if (userInfo .FirstOrDefault()!=null)
                {
                    var emp = userInfo .FirstOrDefault();
                    emp.OfflinePin = pinpswrdbx.Password;
                   await usertable.UpdateAsync(emp);
                }

After updating done we are pushing those changes to the Server.

await App.MobileService.SyncContext.PushAsync();

Here the issue is, the line calling for update the offline pin is not working, means

await usertable.UpdateAsync(emp); is not updating the respective employee information in local table. We are not getting any exception here, it's executing successfully but the offline pin column value is not updating with the user entered pin value.

It is happening only for the rows of data added directly on database and synced to local SQLite DB which are not created within the app, if the record is inserted/created with in the app then that data is updating and am able to push those changes to server as well.

I have to use the existing database and tables which already having data in it and the app should be able to update the data and push the changes back to the server DB.

Can anybody help me where I am missing or doing wrong?


Solution

  • The problem is with the database, it is having case sensitive collation. I used fiddler to track what exactly the mobile service doing while pushing result to server, then I found that in the request URL all the stating letters of the table columns are automatically converting in to capital letters, but in database they are in small letters. May be due to this the data wasn't updated in database and roll backing in local DB as well.

    To overcome this issue I had written the below line of code in DBContext class in mobile service project and did publish.

    modelBuilder.Entity<User>().Property(p => p.Offlinepin).HasColumnName("offlinepin");
    

    After this updating is working. I don't know why Mobile service did not show any error message when push failed instead it roll back the local values.