title: Partition Key for Cosmos DB
body: <p>I am building an restful API using ASP.NET Core with Cosmos DB. There's a GetItemById request that needs a partition key. </p>
  
  <pre><code> public static async Task&lt;T&gt; GetItemAsync(string itemId, string name)
      {
          try
          {
              Document document =
                  await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, itemId),
                      new RequestOptions() { PartitionKey = new PartitionKey(name) });
  
              return (T)(dynamic)document;
          }
          catch (DocumentClientException e)
          {
              if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
              {
                  return null;
              }
              else
              {
                  throw;
              }
          }
      }
  </code></pre>
  
  <p>The GetByIdAsync() function calls this GetItemAsync() method.</p>
  
  <pre><code>    [HttpGet("{itemId}")]
      public async Task&lt;IActionResult&gt; GetByIdAsync(string itemId, string name)
      {
          var item = await DocumentDBRepository&lt;TodoItem&gt;.GetItemAsync(itemId, name);
          if (item == null)
          {
              return NotFound();
          }
          return new ObjectResult(item);
      }
  </code></pre>
  
  <p>The URL would be <a href="http://localhost:54084/api/todo/f323307b-142f-46f3-9072-12f41cc74e87" rel="nofollow noreferrer">http://localhost:54084/api/todo/f323307b-142f-46f3-9072-12f41cc74e87</a></p>
  
  <p>My Azure CosmosDB container looks like this:
  <a href="https://i.stack.imgur.com/mJiDj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mJiDj.png" alt="enter image description here"></a></p>
  
  <p>But when I run this, it gave me an error with </p>
  
  <blockquote>
    <p>{Microsoft.Azure.Documents.DocumentClientException: Partition key
    provided either doesn't correspond to definition in the collection or
    doesn't match partition key field values specified in the document.</p>
  </blockquote>