How to: Avoid Inserting Related Entities?
- by niaher
I have this schema:
I want to insert an Order with OrderItems into database, so I wrote this method:
public void SaveOrder(Order order)
{
using (var repository = new StoreEntities())
{
// Add order.
repository.Orders.AddObject(order);
// Add order items.
foreach (OrderItem orderItem in order.OrderItems)
{
repository.OrderItems.AddObject(orderItem);
}
repository.SaveChanges();
}
}
Everything is inserted just fine, except that new Product records are inserted too, which is not what I want.
What I want is to insert Order and its OrderItems, without going any further down the object graph. How can that be achieved?
Any help is really appreciated, thank you.