Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7175983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:24:55+00:00 2026-05-28T16:24:55+00:00

I have a exceedingly simplistic data model (below). I am having trouble figuring out

  • 0

I have a exceedingly simplistic data model (below). I am having trouble figuring out how I am to get my navigational properties to load from the database. I have no trouble getting them in, but the navigational property does not get set by EF it appears. I have seen several related questions, but they are slightly different or rather involved. I am looking for information on how navigational properties are treated by EF 4.2 (POCO). In the reading I’ve done, I got the impression that I would be able to access objects with foreign keys using navigational properties. Instead, my properties are coming back as either null or empty depending on if I instantiate my collection in the constructor.

public class AnimalDb : DbContext
{
    public static AnimalDb Create(string fileName)
    {
        Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
        return new AnimalDb(fileName);
    }

    private AnimalDb(string fileName) : base(fileName) { }

    public DbSet<Animal> Animals { get; set; }
}

public class Animal
{
    public Animal()
    {
        Id = Guid.NewGuid();
        Traits = new ObservableCollection<Trait>();
    }

    public Guid Id { get; set; }
    public string Species { get; set; }
    public string Name { get; set; }
    public ObservableCollection<Trait> Traits { get; set; }
}

public class Trait
{
    public Trait()
    {
        Id = Guid.NewGuid();
    }

    public Guid Id { get; set; }
    public string Name { get; set; }
}

And here is some (simple) code that uses it:

foreach (var animal in db.Animals)
{
    foreach (var trait in animal.Traits)
    {
        //animal.Traits count is 0, so this does not run.
        //However there are traits in the database, as my populate
        //function is working fine.
        Console.WriteLine("{0} is {1}", animal.Name, trait.Name);
    }
}

—-Edit Answer Summary—-

Using the article and information provided in the answers below, I was able to discover I could either eagerly load using db.Animals.Include() or enable lazy loading. There is a trick to enabling lazy loading and being able to use it though. First to enable lazy loading I added:

db.Configuration.LazyLoadingEnabled = true;

Next I changed my Traits collection in the following manner:

public virtual ObservableCollection<Trait> Traits { get; set; }

Making it virtual allows the automatically generated proxy to lazily load Traits. That’s it! IMHO I think the MSDN docs should shout this load and clear in the POCO EF 4.2 coding conventions. Again thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:24:56+00:00Added an answer on May 28, 2026 at 4:24 pm

    If you don’t use lazy loading you have to explicitly tell EF to load the relation with the Include method:

    foreach (var animal in db.Animals.Include(a => a.Traits))
    {
        foreach (var trait in animal.Traits)
        {
            //...
        }
    }
    

    You can read more about eager loading in this article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to init a big constant data structure but I get the compiler
Have a n-tire web application and search often times out after 30 secs. How
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
I'm on Rails 3.1.1 and I have noticed my app has become exceedingly slow
I have an exceedingly simple USB device. There is no driver provided and instructions
I have to create a mysql query to get a voting distribution of each
I have a list with texts with lengths ranging from 1 character to several
I have an application that holds data referencing 300,000 customers. When a user did
I have an Excel macro that takes an exceedingly long time. It seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.