簡體   English   中英

需要插入時如何在L2E或EF中使用外鍵?

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

我知道在檢索期間,我可以使用Include()加載相關實體( 如何在L2E或EF中使用外鍵? )。 但是當我要保存或插入數據時,如何處理那些引用實體?

您需要將對象掛在其圖形中,然后調用保存更改。 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