【发布时间】:2018-10-24 02:51:08
【问题描述】:
我一直在阅读有关密钥的 Datastore 文档。
在文档中它说
“一个键被存储为一个对象而不是一个值。”
下面是我的一个样本。 ID 是我尝试检索以更新和删除实体的键
显示结果
@page
@using TestApp.Models
@model AllSportsStoreModel
<h2>Index</h2>
<p>
<a asp-page="SportsStore">New Item</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayName("ID")
</th>
<th>
@Html.DisplayName("Name")
</th>
<th>
@Html.DisplayName("Price")
</th>
<th>Edit | Delete</th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.SportsStoreList.Count; i++) {
<tr>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Id)
</td>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].PName)
</td>
<td>
@Html.DisplayFor(model => model.SportsStoreList[i].Price)
</td>
<td>
<a asp-page="EditStore" asp-route-Id="SportsStoreList[i].Id">Edit</a> |
<a asp-page-handler="Delete" asp-route-Id="SportsStoreList[i].Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<br />
代码背后:
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<Item> SportsStoreList { get; set; }
public IActionResult OnGet()
{
SportsStoreList = stores.ReadAll();
return Page();
}
}
}
DataStoreSportsStore.cs
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) => new Key().WithElement("Sports_db", id);
/// <summary>
/// Make a id given a datastore key.
/// </summary>
/// <param name="key">A datastore key</param>
/// <returns>A item id.</returns>
public static long ToId(this Key key) => key.Path.First().Id;
/// <summary>
/// Create a datastore entity with the same values as item.
/// </summary>
/// <param name="item">The item to store in datastore.</param>
/// <returns>A datastore entity.</returns>
public static Entity ToEntity(this Item item) => new Entity()
{
Key = item.Id.ToKey(),
["PName"] = item.PName,
["Price"] = item.Price,
};
/// <summary>
/// Unpack an itemfrom a datastore entity.
/// </summary>
/// <param name="entity">An entity retrieved from datastore.</param>
/// <returns>An Item.</returns>
public static Item ToItem(this Entity entity) => 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<Item> ReadAll()
{
var query = new Query(kind);
var results = _db.RunQuery(query);
return results.Entities.Select(entity => entity.ToItem()).ToList();
}
public void Update(Item item)
{
_db.Update(item.ToEntity());
}
public void Delete(long id)
{
_db.Delete(id.ToKey());
}
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ISportsStore, DatastoreSportsStore>();
services.AddMvc();
}
Item.cs
namespace TestApp.Models
{
public class Item
{
public long Id { get; set; }
public string PName { get; set; }
public string Price { get; set; }
}
}
如何使用 C# 从数据存储实体中检索密钥以更新和删除记录?
【问题讨论】:
-
@Nkosi 类型是 Key
-
您是否阅读过这些文档,它们向您展示了这一点? cloud.google.com/datastore/docs/concepts/… 有一个 c# 选项卡显示它。
-
@DanNick 酷。请务必查看包含的链接。他们非常有帮助。海事组织
-
@Nkosi 我更好地理解了这个例子。但是如何显示数据?我试过这个:foreach (var item in Model.ReadAll()) 它返回一个空豁免错误。
-
快速提问。您更喜欢在项目模型中使用
long还是字符串 id?还是对你没关系?
标签: c# google-app-engine asp.net-core google-cloud-datastore razor-pages