c# - 使用 Azure 移动服务中的脱机表时 CreatedAt 属性为 null

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

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

我有一个 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 属性始终为 null。如果我将属性设置为不可空,则它显示为日期 01/01/0001(基本上是 DateTime.Min)。数据库中的记录具有正确的日期。使用 fiddler 进行测试显示正确的日期正在从 MobileService 中正确序列化

如果我更改为在线环境即

private IMobileServiceTable<TodoItem> todoTable;

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

最佳答案

默认情况下,系统属性(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; }
}

关于c# - 使用 Azure 移动服务中的脱机表时 CreatedAt 属性为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150956/

相关文章:

c# - 在 ASP .NET 应用程序中每 24 小时/每个用户自动触发事件一次

function - 从 VS Code 部署 Azure Function

azure - Azure 中的 Spring Boot 应用程序。无法查看App运行日志

用于创建新 PAT 的 Azure DevOps Rest API

c# - 系统.NotImplementedException : performActions in selenium webdriver C#

c# - 在 Blend 4 中的设计时访问程序集

c# - 如何仅在您的应用中禁用 Web 浏览器 'Click Sound'

c++ - 无法从使用 C API 的程序访问使用 sqlite3 创建的表

java - 当两个java桌面应用程序使用相同的SQLite数据库时如何避免sql异常

android - 如何为 Sqlite 中的 TEXT 列设置默认值?