2

在我的控制器中,我正在遍历项目并将它们保存到我的数据库中。问题是它保存了第一个项目,但没有保存其他项目。我在循环中的“SaveItem()”行上放了一个断点,它每次都会命中它,但对我来说奇怪的是它只通过第一项的方法。

我究竟做错了什么?

public void SubmitItem(Cart cart, ShippingDetails shippingDetails, ProcessedItems processedItem, string orderID)
{
    var cartItems = cart.Lines;
    //CartIndexViewModel cartIndex = new CartIndexViewModel();
    //var customID = cartIndex.OrderID;

    foreach(var item in cartItems)
    {
        processedItem.OrderID = orderID;
        processedItem.ProductID = item.Product.ProductID;
        processedItem.Name = item.Product.Name;
        processedItem.Description = item.Product.Description;
        processedItem.Price = item.Product.Price;
        processedItem.Category = item.Product.Category;
        processedItem.ImageName = item.Product.ImageName;
        processedItem.Image2Name = item.Product.Image2Name;
        processedItem.Image3Name = item.Product.Image3Name;

        processedItem.BuyerName = shippingDetails.Name;
        processedItem.Line1 = shippingDetails.Line1;
        processedItem.Line2 = shippingDetails.Line2;
        processedItem.Line3 = shippingDetails.Line3;
        processedItem.City = shippingDetails.City;
        processedItem.State = shippingDetails.State;
        processedItem.Zip = shippingDetails.Zip;
        processedItem.Country = shippingDetails.Country;

        processedItem.Status = "Submitted";

        processedItems.SaveItem(processedItem);

    } 

}


    public class EFProcessedItemsRepository : IProcessedItems
{
    private  EFDbContext context = new EFDbContext();

    public IQueryable<ProcessedItems> ProcessedItem
    {
        get { return context.ProcessedItems; }
    }

    public void SaveItem(ProcessedItems processedItem)
    {
        if(processedItem.ProcessedID == 0)
        {
            try
            {
                context.ProcessedItems.Add(processedItem);

                context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }
        else
        {
            context.Entry(processedItem).State = EntityState.Modified;

        }
    }

    public void DeleteItem(ProcessedItems processedItem)
    {
        context.ProcessedItems.Remove(processedItem);
        context.SaveChanges();
    }
}

这是已处理项目的类:

public class ProcessedItems
{
    [Key]
    public int ProcessedID { get; set; }

    public string OrderID { get; set; }
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
    public string ImageName { get; set; }
    public string Image2Name { get; set; }
    public string Image3Name { get; set; }
    public string Status { get; set; }

    //shipping
    public string BuyerName { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }


}

界面:

public interface IProcessedItems
{
    IQueryable<ProcessedItems> ProcessedItem { get; }
    void SaveItem(ProcessedItems processedItem);
    void DeleteItem(ProcessedItems processedItem);
}
4

3 回答 3

2

添加所有项目后尝试调用context.SaveChanges(),我认为它应该一次性将它们全部保留。

另一件事要尝试:

重构您的代码,以便 SaveItem 只接受一项要保存、添加并调用 SaveChanges()

循环遍历方法外部的购物车项目,并使用一个项目调用该方法以一次保存。

// set orderID, shippingDetails above

foreach(var item in cartItems)
{
    ProcessedItems processedItem = new ProcessedItems();
    processedItem.OrderID = orderID;
    processedItem.ProductID = item.Product.ProductID;
    processedItem.Name = item.Product.Name;
    processedItem.Description = item.Product.Description;
    processedItem.Price = item.Product.Price;
    processedItem.Category = item.Product.Category;
    processedItem.ImageName = item.Product.ImageName;
    processedItem.Image2Name = item.Product.Image2Name;
    processedItem.Image3Name = item.Product.Image3Name;

    processedItem.BuyerName = shippingDetails.Name;
    processedItem.Line1 = shippingDetails.Line1;
    processedItem.Line2 = shippingDetails.Line2;
    processedItem.Line3 = shippingDetails.Line3;
    processedItem.City = shippingDetails.City;
    processedItem.State = shippingDetails.State;
    processedItem.Zip = shippingDetails.Zip;
    processedItem.Country = shippingDetails.Country;
    SubmitItem(processedItem);

}

public void SubmitItem(ProcessedItems processedItem)
{
    processedItem.Status = "Submitted";
    processedItems.SaveItem(processedItem);
}
于 2012-08-15T14:10:37.870 回答
1

我认为这是因为 processesItem 是每个循环迭代的相同实例。因此,在它通过 SaveItem 一次后,它的 ProcessedID 已设置,因此不会再次被处理。

于 2012-08-15T14:09:02.053 回答
0

我的第一个猜测是你总是存储一个实体,它存储在 processesItem 中,它是一个输入参数。尝试在每个循环上创建新实体,然后保存它。换句话说,您将值分配给输入参数

processedItem.OrderID = orderID; 

然后每次都存储相同的实体,但字段已更改

processedItems.SaveItem(processedItem);
于 2012-08-15T14:07:21.447 回答