宇宙数据库.如何从Upsert操作中检索_ts属性



使用。NET 3 SDK for CosmosDB我对现有文档的upstart操作不会返回文档中存储的任何额外属性,例如_ts属性。我想在我的应用程序中使用此属性。是否必须在模型中包含_ts属性,或者是否有方法从文档中检索_ts?

我必须在模型中包含_ts属性吗?

如果您使用的是类型化模型,那么答案是肯定的。您需要创建一个与响应的_ts属性相对应的属性,否则反序列化程序将无法对此进行反序列化。

另一种选择是使用dynamic类型,并自己转换为模型。例如,您可以执行以下操作:

var model = new Model()
{
Id = "some id",//should serialize to "id",
//other properties of the model
}
var item = (await container.UpsertItemAsync<dynamic>(model)).Resource;
Console.WriteLine(item.ToString());

上面会打印如下内容:

{
"id": "783cf82c-f24c-4031-971d-bd5f604724d5",
"_rid": "xxxx",
"_self": "dbs/xxxx/colls/xxxx/docs/xxxx/",
"_etag": ""xxxx"",
"_attachments": "attachments/",
"_ts": 1621597233,
//other model properties
}

您现在可以提取"_ts";属性。

最新更新