How to: Avoid Inserting Related Entities?
Posted
by
niaher
on Stack Overflow
See other posts from Stack Overflow
or by niaher
Published on 2011-01-02T12:05:17Z
Indexed on
2011/01/02
12:53 UTC
Read the original article
Hit count: 150
c#
|entity-framework-4
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.
© Stack Overflow or respective owner