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.
This line …
… does not throw a
NullReferenceException. It happens later when you accesslocation.Address.Cityor some other property or method ofAddressandAddressis null (which is expected for some locations because it is allowed to have NULL values).You just need to check if
location.Addressisnullor not: