【发布时间】:2020-05-11 20:23:36
【问题描述】:
我有两张桌子CurrentItem 和CurrentItemDataPoints。
CurrentItem 表具有列数据,例如 Id(pk), ItemId, Name, Price 等,其中表 CurrentItemDataPoints 包含每个项目的 30 天数据点。
例如..
itemId: 5, date, price
itemId: 5, date, price
itemId: 5, date, price
...
其中每一个都是CurrentItemDataPoints 中的一行
itemId 与itemId 相关,日期是日期,价格是价格。
现在我构建模型的方式是这样的
public class CurrentItem
{
public int itemId { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public List<CurrentItemDataPoints> The30DayPoints { get; set; } = new List<The30DataPointsModel>();
}
还有 CurrentItemDataPoints
public partial class CurrentItemDataPoints
{
public int itemId { get; set; }
public string date { get; set; }
public long price{ get; set; }
}
我正在尝试更新存储数据点的表,因此每个项目大约有 10 个数据点
using (var db = new RuneMarketDbContext())
{
foreach (var item in buySellData.Take(10))
{
var theItem = db.CurrentPriceTableModels.FirstOrDefault(x => x.Id == item.Value.Id);
if (theItem != null)
{
// Item ID: item.Key
var url = the30daydata + $"{item.Key}.json";
var woop = await client.GetStringAsync(url);
var datapoints = JsonConvert.DeserializeObject<The30DataPointsModel[]>(woop).ToList();
Console.WriteLine($"{item.Value.Id}: Updating item data /w datapoint");
theItem.Id = item.Value.Id;
// ...
theItem.The30DayPoints = datapoints;
db.CurrentPriceTableModels.Update(theItem);
db.SaveChanges();
}
}
}
如您所见,我这样做的方式是这样做theItem.The30DayPoints = datapoints;,但这只是将更多数据附加到表中。
如何正确更新(替换)CurrentItemDataPoints 表中的数据?
【问题讨论】:
标签: c# .net entity-framework entity-framework-core ef-code-first