views:

21

answers:

1

Hello

I have a problem with TPT inheritance + foreign key. I'm working on a web application with multi language support, including translating dynamic content. Here's my tables:

Language Id, Name

ProcessingAgent Id, some other fields

LocalizedProcessingAgent LocalizedProcessingAgentId, Name, Description, LanguageId

As you already guessed, I moved the columns which should be translated from ProcessingAgent to LocalizedProcessingAgent. On EF side, I made a TPT inheritance, so ProcessingAgent is a base class of LocalizedProcessingAgent. I'm new to EF, this is my first project I moved on and one of it's strange behavior really confuses me. When I generate EF classes from DB, LocalizedProcessingAgent has a Navigation Property Language (Because of LanguageId). The problem is that Language property is null after I query the db:

LocalizedProcessingAgentRecord l = db.ProcessingAgents.OfType().First(p => p.Id == 1); //l.Language is null

So the foreign key Navigation Property is not filled for some reason, I don't know why. This happens only when I query derived entities, in this case LocalizedProcessingAgentRecord. Any ideas?

here's EF xml markup: http://dl.dropbox.com/u/3055964/ef.xml

+1  A: 

You should explicitly load this Navigation property.

db.ProcessingAgents.Include("Language").OfType().First(p => p.Id == 1)

Include will result in eager loading of navigation property.

Yury Tarabanko
damn, should I do the same for every single query? Is there a better way to accomplish that? :(
Davita
If you are using ef v4.0 you could enable lazy loading ctx.ContextOptions.LazyLoadingEnable = true. In ef v1.0 there is no transparent lazy loading but you can do explicit lazy loading like this: if(!agent.Language.IsLoaded)agent.Language.Load();
Yury Tarabanko
Thanks yury, your answer was really helpful
Davita
You are welcome :)
Yury Tarabanko