在 Azure 移动服务中使用脱机表时创建属性 null

本文关键字:创建 属性 null 脱机 移动 Azure 服务 | 更新日期: 2023-09-27 18:32:09

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

我有一个移动服务客户端和离线同步表

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)。数据库中的记录具有正确的日期。使用提琴手进行测试显示正确的日期正在从移动服务序列化

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

private IMobileServiceTable<TodoItem> todoTable;

然后一切都按预期工作。创建时间显示正确的日期。我已经尝试了 JsonConverter,如 Carlos 在此处的修复程序所示,但这不起作用,因为日期在进入转换器时已经是 01/01/0001(错误)。我真的需要按日期对记录进行排序,但无法弄清楚为什么这在离线环境中不起作用。任何帮助表示赞赏。

在 Azure 移动服务中使用脱机表时创建属性 null

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

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