Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are createdAt, deleted and updatedAt columns null in MobileServiceSQLiteStore database?

I am using the Azure Mobile App service to synchronise data between a UWP application and Azure. I am using the MobileServiceSQLiteStore class to set up a local Sqlite store to hold data locally, and the MobileServiceClient class to sync local changes to Azure, all as per the quick start on Azure.

My problem is that when I use the InsertAsync method to store data to the local SqlLite store, the createdAt, updatedAt and deleted columns in the store are not being populated. All other columns are being populated as expected. These are the standard columns that the framework expects on my model and I have annotated them as recommended (eg. [CreatedAt])

The linked question highlights the same problem, but for android, and the suggested answer I am already doing, so did not help: Azure Mobile Services Android Offline Example - "createdAt","updatedAt" columns empty

Is anyone able to help?

Here is my code:

using System;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;

namespace Test
{
    public class SyncManagerTest
    {
        public SyncManagerTest()
        {
            var client = new MobileServiceClient(@"https://remote.azurewebsites.net");

            var store = new MobileServiceSQLiteStore("test.db");

            store.DefineTable<Model>();

            client.SyncContext.InitializeAsync(store);

            var table = client.GetSyncTable<Model>();

            table.InsertAsync(new Model
            {
                Name = "Test",
                AnotherDate = DateTimeOffset.Now,
                AnotherBool = true
            });
        }
    }

    public class Model
    {
        [CreatedAt]
        public DateTimeOffset? CreatedAt { get; set; } = DateTimeOffset.Now;

        [Deleted]
        public bool Deleted { get; set; } = false;

        public string Id { get; set; }
        public string Name { get; set; }
        public DateTimeOffset AnotherDate { get; set; }
        public bool AnotherBool { get; set; }
        [UpdatedAt]
        public DateTimeOffset? UpdatedAt { get; set; } = DateTimeOffset.Now;
    }
}

Here is the resulting record in the database:

Database record screenshot

Update After using dotPeek to have a look at the code for InsertAsync I found:

JObject jobject = await this.<>n__1(MobileServiceSyncTable.RemoveSystemPropertiesKeepVersion(serializer.Serialize((object) instance) as JObject));

The method RemoveSystemPropertiesKeepVersion is removing the createdAt, updatedAt and deleted properties from the JObject that is written to the database. Does anyone know why this is being done? I can't see anywhere in the database where date information is kept for operations on the database, how does the sync work? Even the __operations metadata table created by the framework in the Sqlite database doesn't contain a date for each operation.

like image 735
Gavin S Avatar asked Nov 26 '25 08:11

Gavin S


1 Answers

The createdAt and updatedAt fields are updated by the database, not by you. If you push the record to the server, they will be populated for you.

like image 196
Adrian Hall Avatar answered Nov 27 '25 23:11

Adrian Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!