0

This is my second day with EntityFramework and I have another problem:

I'm saving some kind of configuration into my database. This works well: This is my class:

public class Configuration : IConfiguration {.... public ICollection<EmailClassificationPattern> emailClassificationPatterns { get; set; } ... } 

I get two tables dbo.Configurations and dbo.EmailClassificationPatterns. dbo.EmailClassificationPatterns has 4 entries and each references correctly to dbo.Configurations via a field called Configuration_ID.

I am loading the configuration this way:

var configuration = (Configuration)mycontext.Configurations.Where(x => x.configurationName=="Default").FirstOrDefault(); 

Then I am trying to access the member emailClassificationPatterns this was:

 var relevantSubjectList = configuration.emailClassificationPatterns. Select( x => x.subjectPatternForMailCollection); 

But I get a ReferenceNullException because emailClassificationPatterns is NULL. Why does enttity framework not load the referenced members? I don't have the possibility to include the referenced member as IntelliSense doesn't provide .Inlcude() (like this:)

var configuration = (Configuration)mycontext.Configurations.Where(x => x.configurationName=="Default").Include("emailClassificationPattern") 

Do I HAVE to enable eagerLoading? If yes - how? Is there another way to access referenced members and is there some kind of best-practice?

Best regards

Sebastian

1 Answer 1

1

The Include statement comes before Where. This will load "child" entities. Like so:

var configuration = Configuration)mycontext.Configurations.Include("emailClassificationPattern").Where(x => x.configurationName=="Default") 
Sign up to request clarification or add additional context in comments.

3 Comments

Is there another possibility where I don't explicitly have to specify which Child-Memeber-Class should also be loaded? I would have expected that EF recognizes which child-members are needed (based on the code) and loads these element dynamically...
Nope. You have to explicitly load them. his is also pretty smart because in huge relational databases there can be tons of related child-(child-child-etc.)-entities.
But...what you can also do is just load the configuration entities and when you Enumerate certain child entities of Configuration these will be loaded on demand (just-in-time) if I remember it right. Google is your friend! Lots of documentation about (eager)loading in EF ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.