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 7738869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:22:42+00:00 2026-06-01T08:22:42+00:00

I have a question regarding foreign key in entity framework(EF4). Lets say i have

  • 0

I have a question regarding foreign key in entity framework(EF4).

Lets say i have classes :

public class E1
{
   public int Id{get;set;}
   Public string name{get;set;}
}
public class E2
{
   [ForeignKey("e1")]
   public int E1Id{get;set;}

   [ForeignKey("E1Id")]
   public E1 e1{get;set;}

}
public class E3
{
   [ForeignKey("e2")]
   public int E2Id{get;set;}

   [ForeignKey("E2Id")]
   public E2 e2{get;set;}
}
public class E4
{
}

respectively and i suppose i want to access the attribute of class E1 in class E4 by calling via object of E3 (which internally calls E2) as well as E2 calling E1 as shown above but when i try to access the attribute of E1 it throws object null reference exception so whats the possible way to get the attributes of E1?

  • 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-01T08:22:43+00:00Added an answer on June 1, 2026 at 8:22 am

    You need to use either eager loading, lazy loading or explicit loading to force your relation to be loaded.

    Eager loading will load your relations immediately when you load the main entity:

    var e4 = context.E4Set.Include(e => e.E3.E2.E1).First(...);
    

    Lazy loading will load your relations transparently on demand when you first access them (it will trigger separate database query for every accessed navigation property). To make this work all your navigation properties in every entity supporting lazy loading must be virtual.

    Explicit loading demands you to manually execute loading on already loaded entity:

    var e4 = context.EF4Set.First(...);
    var entry = context.Entry(e4);
    entry.Reference(e => e3).Load();
    

    and you can combine it with eager loading:

    entry.Reference(e => e3).Query().Include(e => e.E2.E1).Load();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following POCO classes: public class Parent { public int ID
I have a question regarding dependency injection. say i want to create a class
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have a simple question regarding Entity declaration in JPA. I have an entity
I have a question regarding Foreign Keys in an InnoDB Table. I have 4
I have a general question regarding table indices on foreign keys in database modeling.
I have a question regarding the execution of the following program. #include<stdio.h> int main(void)
I have question regarding macros. How could I cast through macro a template class
I have a question regarding Guice. I have got an interface IMyInterface, the class
I have question regarding the use of function parameters. In the past I have

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.