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

  • Home
  • SEARCH
  • 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 9179179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:44:05+00:00 2026-06-17T17:44:05+00:00

I have a database I created using entity code first In that DB I

  • 0

I have a database I created using entity code first

In that DB I have a structure similar to the following


class ClassA
{
  public virtual int ID {get;set}
  public virtual string some_text {get;set}
  public virtual ClassB B {get;set}
  public virtual ClassC C {get;set}
  ...
}

class ClassB
{
  public virtual int ID {get;set}
  public virtual string some_text {get;set}
  public virtual string some_values {get;set}
  ...
}

class ClassC
{
  public virtual int ID {get;set}
  public virtual string some_text {get;set}
  public virtual string some_values {get;set}
  ...
}
....

Finally I have a context for those objects with all of the interface to query the DB


public class ClassADb : DBContext, IClassADataSource
{
  public DBSet<ClassA> As {get;set}
  public DBSet<ClassB> Bs {get;set}
  public DBSet<ClassC> Cs {get;set}
  ...
}

When I create the DB and explore it I can see that it was created what seems to be correctly:

In the ClassA_Table I see foreign keys for ClassB_ID, ClassC_ID, etc as well as all of the primitive types encapsulated in ClassA (ints, strings, bools, dates, etc)

Also when performing something along the lines of:

ClassB MyB = new ClassB();
//some code to initialize B
...
Bs.Add(MyB)

ClassC MyC = new ClassC();
//some code to initialize C
Cs.Add(MyC);

ClassA MyA = new ClassA();

A.B = MyB;
A.C = MyC;
...

db.SaveChanges();

I again explore the DB and see in Table_A a new row with references to those B and C objects (row id’s corresponding to those objects in the B_table , C_table)

The Problem I am having is that when I do a select from As container , I can retrieve the A object but the nested B and C objects are null

The primitive types are OK (not empty)

Some fixes I tried

The virtual keyword is lazy , so in the constructor of the class accessing the database i did

db.Configuration.ProxyCreationEnabled = false;

But still when doing something along the lines of

A myA = db.As.Find(1);

A.some_text ; // not null
A.B ; //NULL!!!!
A.C ; // NULL

What is causing the entity framework not to fetch the A and B object?

  • 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-06-17T17:44:07+00:00Added an answer on June 17, 2026 at 5:44 pm

    You have to explicitly load related entities explicitly using Include().

    db.As.Include("B").Include("C").Where(a => [some condition]);
    

    In newer version of the Entity Framework there is also a wrapper method around this method accepting a lambda expression avoiding the strings.

    db.As.Include(a => a.B).Include(a => a.C).Where(a => [some condition]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table created using Entity Framework Code First approach. How do
Using the Entity Framework 4. I have created a Code First database in the
Using Code First Entity Framework with .NET MVC 4 I have created a new
Using the Entity Framework Code First paradigm I have defined the following objects and
I have a console application that is using code first Entity Framework 4.3.1. I
I have following C# code that uses Entity Framework Code First approach. The tables
I'm using Entity Framework Code First to generated my database, so I have an
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I have a database that I have created using SQL Server Developer 2008. I
I have a DB that I created using the OOB database initializer, and I

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.