【问题标题】:CreatedAt property null when working with Offline tables in Azure Mobile Services在 Azure 移动服务中使用脱机表时 CreatedAt 属性为 null
【发布时间】:2014-11-26 13:53:12
【问题描述】:

这是一个显示问题的基本示例。

我有一个 MobileServiceClient 和离线 SyncTable

private MobileServiceClient client = new MobileServiceClient("http://localhost:19087/");
private IMobileServiceSyncTable<TodoItem> todoTable;

我在客户端上有一个 TodDoItem.cs

 public class TodoItem
{
    public string Id { get; set; }       
    public DateTimeOffset? CreatedAt { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string Text { get; set; }
    public bool Complete { get; set; }
}

我像这样初始化我的离线数据库

async Task Initialise()
    {
        if (!client.SyncContext.IsInitialized)
        {
            var store = new MobileServiceSQLiteStore("todo.db");
            store.DefineTable<TodoItem>();
            await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
        }
        todoTable = client.GetSyncTable<TodoItem>();
    }

并在此处将在线数据与离线数据同步

async Task RefreshData()
    {
        await todoTable.PullAsync("todo", null);
        var result = await todoTable.ToCollectionAsync();
        listItems.ItemsSource = result;
    }

现在这里的问题是 CreatedAt 属性始终为空。如果我使属性不可为空,则它显示为日期 01/01/0001(基本上是 DateTime.Min)。数据库中的记录具有正确的日期。使用 fiddler 进行测试显示正确的日期正在从 MobileService 进行序列化

如果我更改为在线环境,即

private IMobileServiceTable<TodoItem> todoTable;

然后一切都按预期工作。 CreatedAt 显示正确的日期。 我已经尝试了Carlos' fix here 所示的 JsonConverter,但这不起作用,因为日期已经是 01/01/0001(错误),因为它进入转换器。 我真的需要按日期订购记录并且无法弄清楚为什么这在离线环境中不起作用。任何帮助表示赞赏。

【问题讨论】:

    标签: c# sqlite azure offline azure-mobile-services


    【解决方案1】:

    默认情况下,系统属性(createdAt、version、updatedAt 等)不会在查询中下拉。像这样更新您的定义将使其从服务器请求列并知道将其放入的值。

     public class TodoItem
     {
      public string Id { get; set; }       
      [CreatedAt]
      public DateTimeOffset? CreatedAt { get; set; }
      [UpdatedAt]
      public DateTimeOffset? UpdatedAt { get; set; }
      public string Text { get; set; }
      public bool Complete { get; set; }
    }
    

    【讨论】:

    • 注释仍然出现此错误。它很奇怪,因为这实际上有帮助,但是,我仍然可以复制错误有时。仔细检查了我的代码,我从未将这些属性设置为 null。
    • 更新。添加新记录时发生我的错误。我有一份正确的旧物品清单。如果我在此列表中添加一个新项目(无论如何我手动设置 DateTime),并推送更改,然后从服务表重新轮询值为 null。
    • 这些列的本地值不会发送到服务器/受其尊重。本地列将替换为服务器从 POST 返回到表的内容。如果您 POST 到 /tables/todo?__includeSystemPropeties=* 您的响应中是否包含带有值的 __createdAt 列?
    • 我可以理解服务器会覆盖我的值。我无法理解服务器在我推送后用 null 覆盖我的值。
    • 对,但是为了帮助缩小问题范围,当您执行 POST 时,您的服务器会返回什么?它是否返回了 __createdAt 和 __updatedAt 的正确值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多