R
R
Ruslan Askarov2019-04-04 19:58:02
ASP.NET
Ruslan Askarov, 2019-04-04 19:58:02

How to insert multiple fields at once in Entity Framework asp.net mvc5 database?

Essence of the question:
a web store is being developed, respectively, there is a Basket, in which goods from the Catalog are added. From the Basket, the goods are checked out and transferred to the Order History. The problem is that if more than one product was added to the Cart, then only the first added product will "pass" to the Order History, and the rest will be deleted.
I'm begging for help.

[HttpPost]
        public ActionResult Orders(HistoryOfOrder history ,Order order,Stuff stuff, int Id )
        {
            var user_name = this.User.Identity.Name;
            var find_user = db.Users.FirstOrDefault(u => u.login == user_name);
            var userId = find_user.Id;
            var query2 = 
                         from o in db.Orders
                         where o.UserName == user_name
                         select o;
            var query3 = from h in db.HistoryOfOrders
                         select h;
        

            

            foreach (var all_order in query2)
            {
                if (query2.Count() != 0 )
                {
                    HistoryOfOrder all_history1 = db.HistoryOfOrders.FirstOrDefault(p => history.Id == p.Id);
                    all_order.Adress = order.Adress;
                    all_history1.UserName = all_order.UserName;
                    history.Order_id = all_order.Id;
                    history.NameOfOrder = all_order.NameOfOrder;
                    history.Image = all_order.Image;
                    history.Price = all_order.Price;
                    history.Quantity = all_order.Quantity;
                    history.Summ = all_order.Summ;
                    history.Size = all_order.Size;
                    history.Adress = all_order.Adress;
                    history.OrderTime = System.DateTime.Now;
                    history.Status = "не оформлено";
                    db.Orders.RemoveRange(all_order);
                    db.HistoryOfOrders.Add(history);
                }
                    
            }
           
            db.SaveChanges();
            return RedirectToAction("Orders");
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question