0

I know during the retrieve, I can use Include() to load related Entities (how to use Foreign key in L2E or EF?). but when I want to save or insert data, how to handle those reference Entities?

Community
  • 1
  • 1
5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210
  • possible duplicate of http://stackoverflow.com/questions/480872/entity-framework-setting-a-foreign-key-property – Amy B Apr 14 '10 at 14:34

1 Answers1

1

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();
Amy B
  • 108,202
  • 21
  • 135
  • 185