简体   繁体   中英

how to use Foreign key in L2E or EF when needs insert?

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM