An attempt has been made to Attach or Add an entity that is not new Linq to Sql error

Posted by Collin Oconnor on Stack Overflow See other posts from Stack Overflow or by Collin Oconnor
Published on 2011-03-03T23:21:30Z Indexed on 2011/03/03 23:25 UTC
Read the original article Hit count: 283

Filed under:
|
|

I have a save function for my order entity that looks like this and it breaks on the sumbmitChanges line:

public void SaveOrder ( Order order ) {
                if (order.OrderId == 0)
                    orderTable.InsertOnSubmit(order);
                else if (orderTable.GetOriginalEntityState(order) == null) {
                    orderTable.Attach(order);
                    orderTable.Context.Refresh(RefreshMode.KeepCurrentValues , order);
                }
                orderTable.Context.SubmitChanges();
            }

The order entity contains two other entities; an Address entity and a credit card entity. Now i want these two entities to be null sometimes.

Now my guess for why this is throwing an error is because that both of these entites that are inside order are null. If this is the case, How can I insert an new order into the database with both entities (Address and creditCard) being null.

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc