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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:20:00+00:00 2026-06-05T15:20:00+00:00

I am using Entity Framework and I have two tables, a contact relationship table

  • 0

I am using Entity Framework and I have two tables, a contact relationship table and a contact table. The relationship is many contact relationships to one contact.

In the code below I am trying to get a Contact Relationship and then get the related Contact, however the code below always returns null for Contact.

    [Test]
    public void Mapping_ContactRelationshipsToContacts()
    {
        //Assign
        Guid id = new Guid("118a0d24-cf9d-e111-802d-005056b4000d");

        //Act            
        var result = _db.ContactRelationships.Where(x => x.Id == id).FirstOrDefault().Contact;

        //Assert
        Assert.IsNotNull(result); 
    }

However if I add a line that goes to the DB first and loads the Contact then the Contact Relationship works:

    [Test]
    public void Mapping_ContactRelationshipsToContacts()
    {
        //Assign
        Guid id = new Guid("118a0d24-cf9d-e111-802d-005056b4000d");

        //Act
        var notUsed = _db.Contacts.Where(x => x.Id ==  new Guid("B2A2AB8C-238E-E111-8BF0-005056B4000D")).FirstOrDefault();
        var result = _db.ContactRelationships.Where(x => x.Id == id).FirstOrDefault().Contact;

        //Assert
        Assert.IsNotNull(result);
    }

Notice that I am not using the new variable, all I am doing is ensuring that it is in the DB. Anyone know what causes this behaviour? My mapping for the relationship is:

        modelBuilder.Entity<ContactRelationship>().HasOptional(x => x.Contact)
         .WithMany(o => o.ContactRelationships).HasForeignKey(x => x.ContactID);

I have checked and the ID values are correct and all the data exists.

  • 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-05T15:20:01+00:00Added an answer on June 5, 2026 at 3:20 pm

    You must either make your navigation properties virtual to enable lazy loading…

    public class ContactRelationship
    {
        //...
        public virtual Contact Contact { get; set; }
        //...
    }
    

    … or you must eager load the navigation property using Include:

    var result = _db.ContactRelationships.Include(cr => cr.Contact)
        .Where(x => x.Id == id).FirstOrDefault().Contact;
    

    Your second code snippet works because the Contact is already attached to the context after loading it from the database and because EF establishes relationships automatically between loaded/attached entities. That’s not the case in your first code snippet.

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

Sidebar

Related Questions

I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I am using Entity Framework from .NET 3.5 I have two tables with 0-1
i have two tables in DB: Foo and Bar Using Entity Framework i want
I'm pretty new to MVC 2 using the Entity Framework. I have two tables
I have two tables. I am updating those tables using entity framework. here is
Trying to map the following schema using the Entity Framework. A Customer can have
Is their a way using Entity Framework Code-Only to have an entity that has
I have two tables in my database which have a one-to-one relationship. I want
I am trying to do lookup cross 2 tables using Entity Framework unfortunately I
I'm using Entity Framework 4 in a project, and I have one specific query

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.