我正在尝试学习/制作MVC 5电子商务Web应用程序。除了购买物品外,所有东西都可以正常工作。该项目在数据库中重复,而不是减去所购买产品的数量。我应该如何更改我的代码来做到这一点?
我尝试了多种将商品ID与订单商品ID关联的方法,但是却出现了错误。任何帮助,将不胜感激。谢谢。
public string GetSelectStatement(string JsonDataForSelect)
{
var root = (JObject)JsonConvert.DeserializeObject(JsonDataForSelect);
var query = "";
var query1 = "";
var query2 = "";
string[] tableName = new string[] { };
var items = root.SelectToken("Details").Children().OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var item in items)
{
if (item.Key == "table")
{
tableName = JsonConvert.DeserializeObject<string[]>(item.Value.ToString());
}
else if (item.Key == "fields")
{
var key = item.Value.SelectToken("").OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
var count = 0;
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query1 += string.Format("{0}.{1}", tableName[1], id.Key);
}
else
{
query1 += string.Format("{0}.{1},", tableName[1], id.Key);
}
query = string.Format("select {0} from {1} inner join {2} on {3} where ", query1, tableName[0], tableName[1], query2);
}
}
else if (item.Key == "keys")
{
var key = item.Value.SelectToken("").OfType<JProperty>().ToDictionary(p => p.Name, p => p.Value);
var count = 0;
if (query2 == "")
{
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query2 += string.Format("{0}.{1}={2}.{3}", tableName[0], id.Key, tableName[1], id.Key);
}
else
{
query2 += string.Format("{0}.{1}={2}.{3},", tableName[0], id.Key, tableName[1], id.Key);
}
query = string.Format("select {0} from {1} inner join {2} on {3} where ", query1, tableName[0], tableName[1], query2);
}
}
count = 0;
foreach (var id in key)
{
count++;
if (count == key.Count())
{
query += string.Format("{0}.{1}={2} ", tableName[0], id.Key, id.Value);
}
else
{
query += string.Format("{0}.{1}={2} and ", tableName[0], id.Key, id.Value);
}
}
}
}
return query;
}
答案 0 :(得分:1)
您正在通过致电添加购物车中的物品
orderItems.Add(orderItem)
您的上下文会将其视为新项目。您在这里有两个选择:
_context.Items.Attach(orderItem)
orderItems.Add(_context.Items.Single(x => x.id == orderItem.Id)
检查Working with entity states以获得详细信息。