views:

40

answers:

2

I tried to insert Order Row with the Existing User using LINQ. The program should only insert the Order & Order Details because the User is already exist and I don't want to add the new user for each order.

Instead of that, the program is now inserting Order, OrderDetails together with User. So, I am getting duplicate users in my user table.

I iterate the cartitems and create the orderdetails rows and add them into Order table.

            order o = new order() { orderDate = DateTime.Today};
            foreach (CartItem ci in items)
            {
                orderdetail od = new orderdetail() { itemId = ci.ItemId, itemType = char.Parse(ci.ItemType), price = int.Parse(ci.Price.ToString()) };                    

                o.orderdetails.Add(od);                    
            }

            user u = Users.GetUserByUserId(int.Parse(Session["userId"].ToString()));

            Orders.AddOrder(o, u);

The below is how I add Orders

public static Boolean AddOrder(order o, user u)
        {
            try
            {
                u.orders.Add(o);
                db.orders.InsertOnSubmit(o);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

If I remove db.orders.InsertOnSubmit(o);, the database has no changes and no order is added to the database. If I add that line, it inserts both order, orderdetails and user data.

I am very new to LINQ and Please enlighten me, if you have any idea. Thanks.

A: 

Why does datacontext insert a user? Because it thinks the user doesn't exist in the database.

This code attempts to show datacontext that the user is in the database.

u.orders.Add(o); 
db.orders.InsertOnSubmit(o); 
db.Refresh(RefreshMode.OverwriteCurrentValues, u); 
db.SubmitChanges();

Also check to make sure that user has a primary key defined in the dbml, and that equals is not overridden.

David B
A: 

The problem is because of the different DataContext (db) and I moved Getting User codes under the AddOrder. So, both User and Order are under the same DataContext and ok now.

public static Boolean AddOrder(order o, int userId)
        {
            try
            {
                user u = db.users.Single(us => us.userId == userId);
                u.orders.Add(o);                

                db.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

But another question is ... I want to put the codes for getting entities such as users, orders, products in separate class to reduce the redundancy of codes. I dont want to put everything again and again under every method.

How could I achieve?

I don't want to write the following codes in every place where I want to get the user entity.

user u = db.users.Single(us => us.userId == userId); 

The code for User Entity is only one line. But others are about 4 or 5 lines.

any advice? Thanks.

TTCG