简体   繁体   English

需要插入时如何在L2E或EF中使用外键?

[英]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? ). 我知道在检索期间,我可以使用Include()加载相关实体( 如何在L2E或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. ObjectContext负责其余的工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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