[英]TPT and foreign key issue
我對TPT繼承+外鍵有疑問。 我正在開發具有多語言支持的Web應用程序,包括翻譯動態內容。 這是我的桌子:
Language
=============
Id, Name
ProcessingAgent
====================
Id, some other fields
LocalizedProcessingAgent
=========================================================
LocalizedProcessingAgentId, Name, Description, LanguageId
正如您已經猜到的,我將應從ProcessingAgent
轉換為LocalizedProcessingAgent
的列移動了。 在EF方面,我進行了TPT繼承,因此ProcessingAgent
是LocalizedProcessingAgent
的基類。 我是EF的新手,這是我進行的第一個項目,其中一個奇怪的行為確實使我感到困惑。 當我從數據庫生成EF類時, LocalizedProcessingAgent
具有導航屬性語言(因為LanguageId
)。 問題是查詢數據庫后Language
屬性為null:
LocalizedProcessingAgentRecord l = db.ProcessingAgents
.OfType<LocalizedProcessingAgentRecord>().First(p => p.Id == 1);
//l.Language is null
因此,由於某種原因未填充外鍵導航屬性,我不知道為什么。 僅當我查詢派生實體(在本例中為LocalizedProcessingAgentRecord
時,才會發生這種情況。 有任何想法嗎?
這是EF xml標記: http : //dl.dropbox.com/u/3055964/ef.xml
您應該顯式加載此Navigation屬性。
db.ProcessingAgents.Include("Language").OfType().First(p => p.Id == 1)
Include
將導致急於加載導航屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.