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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:53:58+00:00 2026-06-04T01:53:58+00:00

I use the include Method in my asp.net mvc 2 project with EF 4.0

  • 0

I use the include Method in my asp.net mvc 2 project with EF 4.0

Here is my database structure.

Contact
ContactID
ContactName
AddressID (FK)

Address
AddressID
CodePostal
Street

A contact can contain an address or not.
In some case, I dont want any address. In that case, the AddressID in the contact table will be null.

I try to do this

_db.LocationContacts.Include(“Contact.Address.City.Province.Country”)

Thats give me an error if there is no address associate to the contact.
Is there a way to resolve that situation.

Thanks a lot.

This is the Error Message : System.NullReferenceException: Object reference not set to an instance of an 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-04T01:53:59+00:00Added an answer on June 4, 2026 at 1:53 am

    This line …

    var location = _db.LocationContacts
        .Include("Contact.Address.City.Province.Country").First();
    

    … does not throw a NullReferenceException. It happens later when you access location.Address.City or some other property or method of Address and Address is null (which is expected for some locations because it is allowed to have NULL values).

    You just need to check if location.Address is null or not:

    if (location.Address != null)
    {
        var city = location.Address.City; // OK
    }
    else
    {
        // var city = location.Address.City; // Not OK
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET MVC 2 project on Visual Studio 2010. I want to
I have an ASP.NET MVC 2 application. Web project contains a reference to SomeProject
I'm mixing asp.net webforms and asp.net-mvc . To use webforms I've included routes.IgnoreRoute(Reports/{*pathInfo}); in
Here's my situation. I'm developing an ASP.NET (2.0) app for internal use. In it,
I have a Asp.net MVC 2 project. My start page has following link: http://localhost:2666/Home/Index?SomeID=4f62c452-4513-4dd0-b235-830d721815e4
I am using this in ASp.net MVC Test project: ConfigurationSettings.AppSettings[myconn] and I get green
I work in VS2010, ASP.NET MVC 2 project. The project was completed, it remains
I use ASP.NET MVC 3 Razor, with jquery validation plugin to build a Registration
If I use a join, the Include() method is no longer working, eg: from
I've created a few html helper methods for asp.net mvc that I would like

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.