查询Azure Cosmos DB时并非映射所有属性

时间:2018-07-17 12:56:29

标签: c# azure azure-cosmosdb azure-cosmosdb-sqlapi

我希望将Azure CosmosDB集成到我们的项目中。我为自己设定的目标是创建FAQ部分。我已经创建了表,还向其中添加了三个集合以及一些示例数据。但是,在查询时,我看到不是所有属性都从JSON开始映射,因此查询没有结果。

我的模型类如下;

BasisDocumentDBEntity

//This is a base class with some common shared properties
public abstract class BasisDocumentDBEntity
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "AangemaaktDoor")]
    public string AangemaaktDoor { get; set; }

    [JsonProperty(PropertyName = "AangemaaktOp")]
    public DateTime AangemaaktOp { get; set; }

    [JsonProperty(PropertyName = "GewijzigdOp")]
    public DateTime GewijzigdOp { get; set; }

    [JsonProperty(PropertyName = "GewijzigdDoor")]
    public string GewijzigdDoor { get; set; }
}

FAQGroup

//I've came up with groups for FAQ. A group for example can be 'General' or 'Instruction video's'
public class FAQGroup : BasisDocumentDBEntity
{
    [JsonProperty(PropertyName = "Name")]
    public string Naam { get; set; }

    [JsonProperty(PropertyName = "Sections")]
    public List<FaqSection> Sections { get; set; }
}

常见问题解答

//Each group can contain one or more sections. And each section can contain one or more questions.
public class FaqSection : BasisDocumentDBEntity
{
    [JsonProperty(PropertyName = "Title")]
    public string Title { get; set; }

    [JsonProperty(PropertyName = "GroupId")]
    public string GroupId { get; set; }

    public FAQGroup Group { get; set; }

    [JsonProperty(PropertyName = "Questions")]
    public List<FAQQuestion> Questions { get; set; }
}

常见问题解答

//The actual question with the answer in it.
public class FAQQuestion : BasisDocumentDBEntity
{
    [JsonProperty(PropertyName = "Question")]
    public string Question { get; set; }

    [JsonProperty(PropertyName = "Answer")]
    public string Answer { get; set; }
}

我知道CosmosDB不是关系数据库类型。但是,如果我对this article的理解正确,那应该是可以的。

我的示例数据就这样存储在CosmosDB中;

组:

{
"id": "1",
"Name": "Group 1",
"Sections": [
    1,
    2,
    3
],
"AangemaaktDoor": "user",
"AangemaaktOp": "2018-07-17 08:46",
"GewijzigdDoor": "user",
"GewijzigdOp": "2018-07-17 08:46",
"_rid": "<snip>",
"_self": "<snip>",
"_etag": "<snip>",
"_attachments": "attachments/",
"_ts": 1531818056
}

部分:

{
"id": "1",
"GroupId": "1",
"Title": "Common",
"Questions": [
    1,
    2,
    3
],
"AangemaaktDoor": "user",
"AangemaaktOp": "2018-07-17 08:46",
"GewijzigdDoor": "user",
"GewijzigdOp": "2018-07-17 08:46",
"_rid": "<snip>",
"_self": "<snip>",
"_etag": "<snip>",
"_attachments": "attachments/",
"_ts": 1531810510
}

问题:

{
"id": "1",
"Question": "My First Question is?",
"Answer": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ultrices convallis sapien, sed vestibulum nisl mollis eget. Sed feugiat aliquet orci. Sed pretium feugiat enim, nec lacinia lacus eleifend et. Morbi vitae risus cursus sapien sodales ullamcorper id ut eros. Cras semper ipsum at congue tempus. Fusce hendrerit lorem lorem, non fermentum purus vehicula vel. Nulla aliquam lorem turpis, venenatis hendrerit ligula efficitur eget. Curabitur et erat quis diam interdum vestibulum. Proin congue feugiat dui, a feugiat nisi bibendum eget. Etiam congue orci eget magna efficitur semper. Sed mattis posuere ex, ut venenatis augue condimentum ac. Etiam tincidunt est odio, vitae interdum nibh mollis a. Aliquam id hendrerit dui, et bibendum justo. In hac habitasse platea dictumst.",
"AangemaaktDoor": "user",
"AangemaaktOp": "2018-07-17 08:46",
"GewijzigdDoor": "user",
"GewijzigdOp": "2018-07-17 08:46",
"_rid": "<snip>",
"_self": "<snip>",
"_etag": "<snip>",
"_attachments": "attachments/",
"_ts": 1531810570
}

我在Microsoft文档上使用this article作为参考。但是我无法弄清楚为什么某些属性被填充而有些属性没有被填充。这就是我从存储库中获得的信息;

enter image description here

GroupId和Title字段均不包含任何数据。而且我无法解决这个问题。感觉很明显。有人知道吗?

更新

    public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> 
predicate)
            {
            if (!_isInitialized) throw new InvalidOperationException("Repository must be initialized first!");

            //Query with filter >> returns nothing
            IDocumentQuery<T> query = _client.CreateDocumentQuery<T>(
                UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
                new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
                .Where(predicate)
                .AsDocumentQuery();

            //Query without filter >> returns list with single section but empty 'GroupId' and empty 'Title'
            var xquery = _client.CreateDocumentQuery<T>(
            UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId),
            new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true })
            .ToList();

            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                results.AddRange(await query.ExecuteNextAsync<T>());
            }

            return results;
        }

更新2

    public async Task<SectionDTO> GetFAQOverzicht(EFaqGroup faqGroup)
    {
        var faqSection = new SectionDTO();

        var groupId = ((int) faqGroup).ToString();
        var section = await DocumentDBRepository<FaqSection>.GetItemsAsync(faq => faq.GroupId == groupId);

        return faqSection;
    }

1 个答案:

答案 0 :(得分:1)

CosmosDB不是您已经说过的关系数据库。您使用的CosmosDB SDK不是ORM。只是CosmosDB API的包装。

这意味着除非您使用联接或执行多个查询来进行检索,否则您将无法仅从各自的集合中获取其他对象。

仅仅因为您拥有一个包含数字的GroupId属性,并不意味着Section集合与Group集合具有任何类型的关系。它不是实体框架。对于CosmosDB和SDK来说,它们只是两个独立的集合,它们之间没有关系。

为了执行您想要的操作,您需要执行多个查询以填充要检索的对象(不建议每个子对象一个),或者使用CosmosDB SQL联接到其他两个表并映射检索到的对象结果到您的对象。