2010-12-02 19 views
0

我正在使用Silverlight 4,實體框架4和WCF數據服務。 假設我有一個播放列表對象。該播放列表對象具有屬性以及由0到1導航屬性表示的外鍵給顯示對象。在數據庫中有一個Display_Id列。保存外鍵,但不是WCF數據服務中的整個對象

我試圖保存播放列表並直接設置display_id,而不從數據庫加載整個顯示對象(我從查詢字符串中獲取顯示ID)。 我已經試過:

playlist.Display = new Display() { Id = 3136 }; 
// this SetLink throws an exception that the Display is not yet tracked  
context.SetLink(playlist.Display, "Display", playlist); 
// or i've tried, but get an error: Entities in 'EDM.Displays' participate in the //'DisplayX' relationship. 0 related 'X' were found. 1 'X' is expected 
context.AddToDisplays(playlist.Display); 
context.SetLink(playlist, "Display", playlist.Display); 

我需要改變我的EDM還是有辦法做到這一點在客戶端?

回答

1

首先您需要連接對象的ObjectSet

Display d = new Display{ id = 3136 }; 
context.Displays.Attach(d); 

它會在本地執行,而不調用數據庫,然後就可以正常工作,因爲你需要

的還有另一種方式:對象播放列表中包含的對象帶有屬性EntityKey的DysplayReference,如果它是null然後創建它,否則只是替換EntityKeyValues中的鍵

+0

我相信AddToDisplays方法將它附加到上下文中。 – Aligned 2010-12-07 14:13:24

相關問題