An attempt has been made to Attach or Add an entity that is not new Linq to Sql error
- by Collin Oconnor
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.