views:

70

answers:

3

I know during the retrieve, I can use Include() to load related Entities (http://stackoverflow.com/questions/2632323/how-to-use-foreign-key-in-l2e-or-ef). but when I want to save or insert data, how to handle those reference Entities?

+1  A: 

http://blogs.msdn.com/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx

mynameiscoffey
that is for .Net 4. I am currently in .Net 3.5 and cannot do that
5YrsLaterDBA
+1  A: 

You need to hang the object within its graph, and call save changes. ObjectContext takes care of the rest.

Customer customer = myObjectContext.Single(c => c.Name == "Bob");

//new up an Order instance that has never been in the database.
Order order = GetOrderForCar();

//Add order to the Orders ObjectSet of a Customer
// This connects order to our ObjectContext.
customer.Orders.Add(order);

myObjectContext.SaveChanges();
David B