我的模特班是这样的;
基本文档实体
//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; }
}
常见问题组
//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
}
我在用
this article
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;
}
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;
}