【发布时间】:2023-04-04 03:57:02
【问题描述】:
我有一个 Xamarin 应用程序,我在 Azure 上使用 Cosmos DB,在 DB 端我有这样的文档:
{
"_id" : ObjectId("5b84142d07bf8638bcf7a089"),
"business_id" : "5b81309a7dfa952bb4036f55",
"contentType" : "image",
"media" : "https://fimgs.net/images/perfume/375x500.30796.jpg",
"id" : "22155414-ee2b-3191-c180-3bc9d40db16b"
}
在 Xamarin 方面,我使用以下代码:
List<TempObject> TempObjects = new List<TempObject>();
Uri collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId);
var query = _client.CreateDocumentQuery<TempObject>(collectionUri).AsDocumentQuery();
while (query.HasMoreResults)
{
TempObjects.AddRange( await query.ExecuteNextAsync<TempObject>() );
}
这是数据类定义:
class TempObject
{
//[JsonProperty(PropertyName = "_id")]
//public string _id { get; set; }
[JsonProperty(PropertyName = "business_id")]
public string business_id { get; set; }
[JsonProperty(PropertyName = "contentType")]
public string contentType { get; set; }
[JsonProperty(PropertyName = "media")]
public string media { get; set; }
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
}
问题是它返回正确数量的对象,但所有属性都是空的!
已使用默认设置创建集合,数据库几乎为空,此集合中只有 2 个文档!
代码似乎完全符合我找到的所有教程! 主要是这个:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/cosmosdb/consuming
此外,当我将本地 CosmosDB 模拟器与同一个数据库一起使用时,它可以工作,但在 Azure 上时就不行了!这是否意味着我的代码是正确的并且问题出在 Azure 端?
对这个问题有任何想法吗?或者调试此类问题的方法?
谢谢
编辑:我也尝试过使用 Cosmonaut API,代码如下:
var cosmosSettings = new CosmosStoreSettings("soclozecosmosdb", "https://soclozedb.documents.azure.com:443/", "E2ipML2QWNVjWITKhX0K0pn7ooCWxbkEk0xkQIC6QIWQCmMjsLU3D2SRTLaIk0dB3bm4k4mWhlpYYpbgsrk2xw==");
ICosmosStore<TempObject> store = new CosmosStore<TempObject>(cosmosSettings);
var users = store.Query().ToList();
但是,查询返回 0 个对象!
【问题讨论】:
-
如果我错了,请纠正我,但
_id属性应如下所示:"_id": "ObjectId("5b84142d07bf8638bcf7a089")",在 ObjectId 周围带有适当的双引号。是这样吗?除此之外,它看起来像一个映射问题,但你的类看起来是正确的。 -
我不知道ObjectId ...这一行是自动添加的,我根本不知道它是什么!谢谢
-
你在使用 MongoDB api 吗?
-
我说的是 CosmosDB Mongo API。
-
您需要将
ICosmosStore<TempObject> store = new CosmosStore<TempObject>(cosmosSettings);更改为ICosmosStore<TempObject> store = new CosmosStore<TempObject>(cosmosSettings, "publication");以将其指向集合。
标签: c# linq azure xamarin azure-cosmosdb