LINQ to SQL - Insert yielding strange behavior.
Posted
by Isaac
on Stack Overflow
See other posts from Stack Overflow
or by Isaac
Published on 2010-03-28T21:06:28Z
Indexed on
2010/03/28
21:13 UTC
Read the original article
Hit count: 228
linq-to-sql
|c#
Hi,
I'm trying to insert several newly created items to the database. I have a LINQ2SQL generated class called "Order".
Inside order, there's a property called "OrderItems" which is also generated by LINQ2SQL and represents the Items of that Order.
So far so good. The problem I'm having right now, is when I try to add more than one newly created OrderItem inside Order.
I.E:
Order o = orderWorker.GetById( 10 );
for( int i=0; i < 5; ++i ) {
OrderItem oi =new OrderItem {
Order = order,
Price = 100,
ShippingPrice = 100,
ShippingMethod = ...
};
o.OrderItems.Add( oi );
}
context.SubmitChanges();
Unfortunately, only a single entity is being added. Yes, I checked the generated SQL by adding Context.Log = Console.Out, and yes, only one statement was created.
Any clues? By the way I know I'm not using InsertOnSubmit, by the documentation says:
You can explicitly request Inserts by using InsertOnSubmit. Alternatively, LINQ to SQL can infer Inserts by finding objects connected to one of the known objects that must be updated. For example, if you add an Untracked object to an EntitySet(TEntity) or set an EntityRef(TEntity) to an Untracked object, you make the Untracked object reachable by way of tracked objects in the graph. While processing SubmitChanges, LINQ to SQL traverses the tracked objects and discovers any reachable persistent objects that are not tracked. Such objects are candidates for insertion into the database.
Thank you very much for your time.
© Stack Overflow or respective owner