mkell
mkell

Reputation: 731

why won't Property values save to database in mvc 4 application.

I've being trying to expand on a tutorial (Pro ASP.NET 4, sportsStore). I want to include an orders Table to track all order made.

I created the table in Sql and a model to match ....

      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; }
}

}

this is a combination of properties from two other models plus an OrderID.

I added the following to EFDbContext -

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

And in my 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();
                        }
                     }

Nothing crashes , the program works fine and I can see the values getting to the SaveOrder Method in the EmailOrderProcessor, but nothing appears in the Database....What am I missing ? I didn't have this problem when saving to the products table. Thanks for reading.

Upvotes: 0

Views: 120

Answers (2)

user1666620
user1666620

Reputation: 4808

You need to place

context.Orders.Add(dbEntry);

before the context.SaveChanges(); call, otherwise it is never being loaded into the DbContext.

You should also place the dbEntry declaration into the foreach loop.

Upvotes: 3

Jason P
Jason P

Reputation: 27012

You should create a new order with each iteration of the loop, and you aren't adding the orders to the context:

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

Upvotes: 1

Related Questions