为什么在mvc 4应用程序中将属性值保存到数据库中。

时间:2014-10-21 16:51:34

标签: c# asp.net-mvc-4 razor

我正在尝试扩展教程(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方法的值,但数据库中没有任何内容......我缺少什么?保存到产品表时,我没有遇到此问题。谢谢你的阅读。

2 个答案:

答案 0 :(得分:3)

你需要放置

context.Orders.Add(dbEntry);
context.SaveChanges();调用之前

,否则它永远不会被加载到DbContext中。

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

答案 1 :(得分:1)

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

 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();