Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,
I have some problem when i am trying to save data in database using entityframework

In my database there are two tables for example :- tables Order and OrderDetails,
OrderId is Forain Key in OrderDetails..

now my Problem Is As Follows..
i am calling function GenerateOrder()


C#
public void GenerateOrder()
       {
using (TransactionScope transactionscope = new TransactionScope())
            {
                bool success = false;
                try
                {
                    newOrder = new Order(CustomerNAme, ShippingAddress, BillingAddress, OrderDate, DiscountCode);

                    long oid = newOrder.PersistOrder();

                    newOrder.AddItem(oid ,1, 12, (decimal)120, 12);
                    newOrder.AddItem(oid, 1, 12, (decimal)130, 12);
                    newOrder.AddItem(oid, 1, 12, (decimal)140, 12);
                    newOrder.AddItem(oid, 1, 12, (decimal)150, 12);
                   
 newOrder.InsertOrderDetails();
                    //foreach (CartItem cartitem in items)
                    //{

                    //}
                    transactionscope.Complete();
                    success = true;
                   //  return newOrder;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occured. "+ "The operation cannot be retried." + ex.Message);                
                }
                if (success)
                    db.SaveChanges();
                else
                    Console.WriteLine("The operation could not be completed");

                // Dispose the object db.
                db.Dispose();          
           
}


C#
public void AddItem(long orderid,int itemID,int quantity,decimal price,short discountcode)
      {
          sc_OrderDetails item = new sc_OrderDetails();

          item.ItemId = itemID;
          item.Quantity = quantity;
          item.Price = price;
          item.DiscountCode = discountcode;
          item.OrderId = orderid;

          orderhead.sc_OrderDetails.Add(item);

      }


C#
public long PersistOrder()
      {
          db.sc_Order.Add(orderhead);
          db.SaveChanges();

          long id = (orderhead.OrderId);
          return id;
      }

      public void InsertOrderDetails()
      {
          foreach (sc_OrderDetails orderdetails in orderhead.sc_OrderDetails)
          {
              db.sc_OrderDetails.Add(orderdetails);
              db.SaveChanges();
          }
      }


first i save the
orders then insert itemdetails in orderdetails table, first it is collected in sc_OrderDetails list then i call insertOrderDetails() then records are saved but if i am saving 2 or more records then after 1st record all record repeats two times..

orderdetailid| orderid |itemid | Quantity | Price Discount
61 |26 |1 |12 |120.00 12
62 |26 |1 |12 |130.00 12
63 |26 |1 |12 |140.00 12
64 |26 |1 |12 |150.00 12
65 |26 |1 |12 |130.00 12
66 |26 |1 |12 |140.00 12
67 |26 |1 |12 |150.00 12


the boalded records are repeated records....please give me regarding solution as soon as possible

thanks in advance...
Posted
Comments

1 solution

Try these,

C#
using (TransactionScope transactionscope = new TransactionScope())
{
    long oid = newOrder.PersistOrder();
    Order objOrder=new order();
    //Insert content into order table
    objOrder.savechanges();

    OrderDetails objdetail=new OrderDetails(); 
   //Insert content into Orderdetails table
    objdetail.savechanges();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900