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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:23:24+00:00 2026-05-23T21:23:24+00:00

I am using Entity Framework 4.1 – Code First. I store user’s addresses as

  • 0

I am using Entity Framework 4.1 – Code First. I store user’s addresses as follows:

Country -> District -> City -> -> Area -> Address

That only applies to addresses inside my country where I provide all the available Districts, Cities and Areas in the country so the user can choose from. And then he fills his address details which are stored in the Addresses table.

In order to make this clearer, take a look at the following:

public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    [MaxLength(500)]
    public string Details { get; set; }
    public bool IsDefaultAddress { get; set; }
    public string CountryName { get; set; }
    public Area Area { get; set; }
    public virtual User User { get; set; }
}

public class Area
{
    public int Id { get; set; }
    public string GoogleName { get; set; }
    public string FamiliarName { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual City City { get; set; }

    public string GetName
    {
        get { return FamiliarName ?? GoogleName; }
    }
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual District District { get; set; }
    public virtual ICollection<Area> Areas { get; set; }
}

public class District
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<City> Cities { get; set; }
    public virtual Country Country { get; set; }
}

public class Country
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<District> Districts { get; set; }
}

I noticed that whether I use the virtual keyword or not, nothing changes. Area is always null for some reason.

How can I retrieve an address and have all the other info (area, city, district and country) objects not be null?

  • 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-23T21:23:25+00:00Added an answer on May 23, 2026 at 9:23 pm

    If you know that you will need them simply use eager loading:

    var address = context.Address.Include(a => a.Area.City.District.Country).Where(...);
    

    If lazy loading doesn’t work when you mark Area virtual then something else is wrong.

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

Sidebar

Related Questions

How to declare a one to one relationship using Entity Framework 4 Code First
Using Entity Framework Code First CTP5, how do I create a primary key column
Using Entity Framework 4 and code first how would I create a model that
When creating a database using Entity Framework code-first, a lot of the database model
I am using Entity Framework 4.1 code first+MVC3 and the inheritence stratagy that I
I am using Entity Framework Code First and ran into a small road block.
I am using Entity Framework 4.1 code first and ASP.NET MVC 3 and I
I am using Entity Framework code first and ASP.NET MVC 3. I have the
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
I'm using Entity Framework. In my database are stored persons and country of their

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.