为什么获胜';t属性值保存到mvc 4应用程序中的数据库中

本文关键字:mvc 应用程序 保存 数据库 获胜 属性 为什么 | 更新日期: 2023-09-27 18:27:17

我正在尝试扩展教程(Pro ASP.NET 4,sportsStore)。我想包括一个订单表来跟踪所有订单。

我用Sql创建了表,并创建了一个匹配的模型。。。。

      public class Orders
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int OrderId { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
    public decimal OrderTotal { get; set; }
    public string Name { 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 bool GiftWrap { get; set; }
}

}

这是来自另外两个模型的属性加上一个OrderID的组合。

我将以下内容添加到EFDbContext-

  public DbSet<Orders> Orders { get; set; }

在我的NinjectControllerFactory-

     ninjectKernel.Bind<IOrderProcessor>()
        .To<EmailOrderProcessor>()
        .WithConstructorArgument("settings", emailSettings);

IOrderProcessor-

   public interface IOrderProcessor
{
    void ProcessOrder(Cart cart, ShippingDetails shippingDetails);
    void SaveOrder(Cart cart, ShippingDetails shippingDetails);
}

EmailOrderProcessor-

   public void SaveOrder(Cart cart, ShippingDetails shippingInfo)
                {
                    Orders dbEntry = new Orders();
                        foreach (var line in cart.Lines)
                        {
                            dbEntry.ProductID = line.Product.ProductID;
                            dbEntry.Quantity = line.Quantity;
                            dbEntry.OrderTotal = line.Quantity * line.Product.Price;
                            dbEntry.Name = shippingInfo.Name;
                            dbEntry.Line1 = shippingInfo.Line1;
                            dbEntry.Line2 = shippingInfo.Line2;
                            dbEntry.Line3 = shippingInfo.Line3;
                            dbEntry.City = shippingInfo.City;
                            dbEntry.State = shippingInfo.State;
                            dbEntry.Zip = shippingInfo.Zip;
                            dbEntry.Country = shippingInfo.Country;
                            dbEntry.GiftWrap = shippingInfo.GiftWrap;
                            context.SaveChanges();
                        }
                     }

没有任何崩溃,程序运行良好,我可以看到EmailOrderProcessor中的SaveOrder方法的值,但数据库中没有任何显示。。。。我错过了什么?我在保存到产品表时没有遇到这个问题。感谢阅读。

为什么获胜';t属性值保存到mvc 4应用程序中的数据库中

您需要放置

context.Orders.Add(dbEntry);

context.SaveChanges();调用之前,否则它永远不会加载到DbContext中。

您还应该将dbEntry声明放入foreach循环中。

您应该在循环的每次迭代中创建一个新的订单,而不是将订单添加到上下文中:

 foreach(var line in cart.Lines) {
     Orders dbEntry = new Orders();
     dbEntry.ProductID = line.Product.ProductID;
     dbEntry.Quantity = line.Quantity;
     dbEntry.OrderTotal = line.Quantity * line.Product.Price;
     dbEntry.Name = shippingInfo.Name;
     dbEntry.Line1 = shippingInfo.Line1;
     dbEntry.Line2 = shippingInfo.Line2;
     dbEntry.Line3 = shippingInfo.Line3;
     dbEntry.City = shippingInfo.City;
     dbEntry.State = shippingInfo.State;
     dbEntry.Zip = shippingInfo.Zip;
     dbEntry.Country = shippingInfo.Country;
     dbEntry.GiftWrap = shippingInfo.GiftWrap;
     context.Orders.Add(dbEntry);
 }
context.SaveChanges();