title: Retrieve Key from Datastore (Update & Delete Entity)
body: <p>I have been reading the Datastore documentation about keys. </p>
  
  <p>In the documentation it says </p>
  
  <blockquote>
    <p>"a key is stored as an object not as a value." </p>
  </blockquote>
  
  <p>This is the result
  <a href="https://i.stack.imgur.com/tJXeS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tJXeS.png" alt="enter image description here"></a></p>
  
  <p>Below is a sample from my kind. ID is the key I am trying to retrieve to update and delete an entity</p>
  
  <p><a href="https://i.stack.imgur.com/a65rO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a65rO.png" alt="Sample Data"></a></p>
  
  <p>Display the results</p>
  
  <pre><code>@page
  @using TestApp.Models
  @model AllSportsStoreModel
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      &lt;a asp-page="SportsStore"&gt;New Item&lt;/a&gt;
  &lt;/p&gt;
      &lt;table class="table"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.DisplayName("ID")
                  &lt;/th&gt;
                  &lt;th&gt;
                      @Html.DisplayName("Name")
                  &lt;/th&gt;
                  &lt;th&gt;
                      @Html.DisplayName("Price")
                  &lt;/th&gt;
                  &lt;th&gt;Edit | Delete&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
  @for (var i = 0; i &lt; Model.SportsStoreList.Count; i++) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(model =&gt; model.SportsStoreList[i].Id)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(model =&gt; model.SportsStoreList[i].PName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(model =&gt; model.SportsStoreList[i].Price)
          &lt;/td&gt;
          &lt;td&gt;
              &lt;a asp-page="EditStore" asp-route-Id="SportsStoreList[i].Id"&gt;Edit&lt;/a&gt; |
              &lt;a asp-page-handler="Delete" asp-route-Id="SportsStoreList[i].Id"&gt;Delete&lt;/a&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  }
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;br /&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Threading.Tasks;
  using Google.Cloud.Datastore.V1;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using TestApp.Models;
  
  namespace TestApp.Pages
  {
      public class AllSportsStoreModel : PageModel
      {
          private readonly ISportsStore stores;
  
          public AllSportsStoreModel(ISportsStore stores)
          {
              this.stores = stores;
          }
  
          [BindProperty]
          public List&lt;Item&gt; SportsStoreList { get; set; }
  
          public IActionResult OnGet()
          {
              SportsStoreList = stores.ReadAll();
              return Page();
          }
  
      }
  }
  </code></pre>
  
  <p>DataStoreSportsStore.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Google.Cloud.Datastore.V1;
  
  
  namespace TestApp.Models
  {
      public static class DatastoreBookStoreExtensionMethods
      {
          public static Key ToKey(this long id) =&gt; new Key().WithElement("Sports_db", id);
  
          /// &lt;summary&gt;
          /// Make a id given a datastore key.
          /// &lt;/summary&gt;
          /// &lt;param name="key"&gt;A datastore key&lt;/param&gt;
          /// &lt;returns&gt;A item id.&lt;/returns&gt;
          public static long ToId(this Key key) =&gt; key.Path.First().Id;
  
          /// &lt;summary&gt;
          /// Create a datastore entity with the same values as item.
          /// &lt;/summary&gt;
          /// &lt;param name="item"&gt;The item to store in datastore.&lt;/param&gt;
          /// &lt;returns&gt;A datastore entity.&lt;/returns&gt;
          public static Entity ToEntity(this Item item) =&gt; new Entity()
          {
              Key = item.Id.ToKey(),
              ["PName"] = item.PName,
              ["Price"] = item.Price,
          };
  
          /// &lt;summary&gt;
          /// Unpack an itemfrom a datastore entity.
          /// &lt;/summary&gt;
          /// &lt;param name="entity"&gt;An entity retrieved from datastore.&lt;/param&gt;
          /// &lt;returns&gt;An Item.&lt;/returns&gt;
          public static Item ToItem(this Entity entity) =&gt; new Item()
          {
              Id = entity.Key.Path.First().Id,
              PName = (string)entity["PName"],
              Price = (string)entity["Price"]
          };
      }
  
  
      public class DatastoreSportsStore : ISportsStore
      {
          string kind = "Sports_db";
          private readonly DatastoreDb _db;
  
          public DatastoreSportsStore()
          {
              _db = DatastoreDb.Create("projectid");
          }
  
          public void Create(Item item)
          {
              var entity = item.ToEntity();
              entity.Key = _db.CreateKeyFactory(kind).CreateIncompleteKey();
              var keys = _db.Insert(new[] { entity });
              item.Id = keys.First().Path.First().Id;
          }
  
          public Item Read(long id)
          {
              return _db.Lookup(id.ToKey())?.ToItem();
          }
  
          public List&lt;Item&gt; ReadAll()
          {
              var query = new Query(kind);
              var results = _db.RunQuery(query);
              return results.Entities.Select(entity =&gt; entity.ToItem()).ToList();
          }
  
          public void Update(Item item)
          {
              _db.Update(item.ToEntity());
          }
  
          public void Delete(long id)
          {
              _db.Delete(id.ToKey());
          }
  
      }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddScoped&lt;ISportsStore, DatastoreSportsStore&gt;();
      services.AddMvc();
  }
  </code></pre>
  
  <p>Item.cs</p>
  
  <pre><code>namespace TestApp.Models
  {
      public class Item
      {
          public long Id { get; set; }
          public string PName { get; set; }
          public string Price { get; set; }
      }
  }
  </code></pre>
  
  <p>How does one retrieve a key from the Datastore Entity to update and delete records using C#?</p>