2016-04-15 17 views
5

Ho la seguente stored procedure che trova un oggetto solo da id.Calling DocumentDb stored procedure da .net

function sample(id) { 
    var context = getContext(); 
    var response = context.getResponse(); 
    var collection = context.getCollection(); 

    var findObject = "SELECT * FROM Objects o where o.userId='" + id +"'"; 

    // Query documents and take 1st item. 
    var isAccepted = collection.queryDocuments(
     collection.getSelfLink(), 
     findObject, 

     function (err, feed, options) { 
      if (err) throw err; 

      // Check the feed and if empty, set the body to 'no docs found', 
      // else take 1st element from feed 
      if (!feed || !feed.length) throw new Error("Object not found"); 
      else response.setBody(feed[0]); 
     }); 

    if (!isAccepted) throw new Error('The query was not accepted by the server.'); 
} 

E questa è la mia classe C# che si estende documento

public class UserPoints: Document 
{ 
    [JsonProperty("userId")] 
    public Guid UserId; 

    [JsonProperty("remainingPoints")] 
    public int RemainingPoints; 
} 

Nella mia funzione principale, che io chiamo la stored procedure sopra e si aspettano di restituire i UserPoints oggetto per l'ID utente.

Esempio:

UserPoints points = new UserPoints() 
{ 
    UserId = Guid.NewGuid(), 
    RemainingPoints = 1000 
}; 

await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collection), points); 
points.Dump(); 

var response = await client.ExecuteStoredProcedureAsync<UserPoints>(UriFactory.CreateStoredProcedureUri(databaseName, collection, storedProcedureName), points.UserId.ToString()); 
response.Response.Dump(); 

ottengo la seguente eccezione Impossibile eseguire il cast oggetto di tipo 'Microsoft.Azure.Documents.Document' digitare '' UserPoints quando eseguo la stored procedure

Funziona tutto bene se ho appena smesso di estendere la classe Document base, ma poi non ho accesso alla proprietà SelfLink di cui ho bisogno per gli aggiornamenti. Sto facendo qualcosa di sbagliato? Se ExecuteStoredProcedureAsync assume un tipo forte, non dovrebbe essere in grado di digitare cast e restituire l'oggetto di quel tipo?

+1

PS. non hai più bisogno di SelfLink per qualsiasi operazione in DocumentDB. Ora puoi semplicemente usare l'id della risorsa a cui stai cercando di fare riferimento. Ad esempio ReplaceDocumentAsync (UriFactory.CreateDocumentUri ("ID del mio database", "ID della mia raccolta", "ID del mio doc"), updated_doc_object). Per maggiori informazioni su questo rimanda a https://azure.microsoft.com/en-us/blog/azure-documentdb-bids-fond-farewell-to-self-links/ –

risposta

0

Questo è un bug nell'SDK di DocumentDB .NET. Stiamo esaminando una soluzione.

Nota che come soluzione alternativa finché la correzione non viene rilasciata, non è necessario derivare da Documento per archiviare documenti in DocumentDB. Se hai bisogno di accedere ad alcune proprietà interne come Id, puoi aggiungere tali proprietà al tuo oggetto, ad es. as

[JsonProperty("id")] public string Id {get; set;} 
Problemi correlati