【问题标题】:Why are createdAt, deleted and updatedAt columns null in MobileServiceSQLiteStore database?为什么 MobileServiceSQLiteStore 数据库中的 createdAt、deleted 和 updatedAt 列为空?
【发布时间】:2017-01-27 10:28:57
【问题描述】:

我正在使用 Azure 移动应用服务在 UWP 应用程序和 Azure 之间同步数据。我正在使用 MobileServiceSQLiteStore 类设置本地 Sqlite 存储以在本地保存数据,并使用 MobileServiceClient 类将本地更改同步到 Azure,所有这些都按照 Azure 上的快速入门。

我的问题是,当我使用 InsertAsync 方法将数据存储到本地 SqlLite 存储时,存储中的 createdAt、updatedAt 和 deleted 列没有被填充。所有其他列都按预期填充。这些是框架期望在我的模型上使用的标准列,我已按照建议对它们进行了注释(例如 [CreatedAt])

链接的问题突出了同样的问题,但对于 android,以及我已经在做的建议答案,所以没有帮助:Azure Mobile Services Android Offline Example - "createdAt","updatedAt" columns empty

有人可以帮忙吗?

这是我的代码:

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;
    }
}

这是数据库中的结果记录:

更新 在使用 dotPeek 查看 InsertAsync 的代码后,我发现:

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

方法 RemoveSystemPropertiesKeepVersion 正在从写入数据库的 JObject 中删除 createdAt、updatedAt 和 deleted 属性。有谁知道为什么要这样做?我在数据库中看不到为数据库操作保存日期信息的任何地方,同步是如何工作的?即使是框架在Sqlite数据库中创建的__operations元数据表也不包含每个操作的日期。

【问题讨论】:

    标签: .net sqlite azure uwp azure-mobile-services


    【解决方案1】:

    createdAt 和 updatedAt 字段由数据库更新,而不是由您更新。如果您将记录推送到服务器,它们将为您填充。

    【讨论】:

    • 感谢 Adrian,但框架如何知道记录的创建时间?如果下一次推送是在本地创建记录后一周,并且在这之间有很多应用程序重新启动,该怎么办。我没有看到这个日期信息存储在哪里?还是说 createdAt 和 updatedAt 设置为推送日期?那么删除的列呢?框架如何知道记录是否被删除?
    • createdAt 和 updatedAt 在服务器端数据库中存储和更新。默认情况下,deleted 为 false(即未删除),但作为任何其他字段处理。
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2022-12-02
    • 2018-10-08
    相关资源
    最近更新 更多