簡體   English   中英

使用集合和文檔對象的documentDB

[英]documentDB using collection and document to object

我有ObjectModel包含例如2類Student和Course。

我想用documentDB保存這2個集合(一個課程集合一個,學生一個集合),我需要在documentDB DB中創建2個不同的集合嗎? ,或者我只能使用一個收藏集?

當我想從文檔轉換為ObjectModel.Student時,另一個問題是我不想使用動態轉換(實時性能問題)。

public static async Task<ObjectModel.Student> GetGroupAsyncByID(string id)
    {
        try
        {
            Document document = await _client.ReadDocumentAsync(UriFactory.CreateDocumentUri(cDatabaseId, cAllCollections[0], id));
            return (ObjectModel.Student)document; // compile error 
        }
        catch (DocumentClientException e)
        {
            if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return null;
            }
            else
            {
                throw;
            }
        }
    }

我的學生班

public class Student 
    {
        #region Fields
        [JsonProperty(PropertyName = "id")]
        public string ID { get; set; }
        public String StudentName { get; set; }

}

我如何才能從文檔投射到學生對象?

謝謝!

DocumentDB是架構專用的,是的,您可以將Course和Student對象保存在一個集合中。 為了輕松判斷應將文檔強制轉換為哪個類,您可以在兩個對象中定義“ Type”屬性,對於Course對象定義1,對於Student對象定義2。

Document對象實現動態合同。 您可以按Student s =(動力學)文檔強制轉換為Student。 這里的性能影響非常小,因為文檔已經包含JSON對象的PropertyBag,它將只將“ Student”的屬性設置為PropertyBag中的值。

如果您想自己處理,ResourceResponse還公開原始流,以便您可以自己處理反序列化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM