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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:02:05+00:00 2026-06-02T23:02:05+00:00

This is the first time I am making a project in C# with the

  • 0

This is the first time I am making a project in C# with the Entity Framework 4.3.1. I am having troubles with getting all the data from the Tournament table, the Address to be precise.

First of all, here is my Code First code. When I run this, the 2 tables are created correctly with the correct relation. I have a lot more columns defined in those classes, but for this example I only show a few.

public class EFDbContext : DbContext
{
    public EFDbContext()
        : base("ApplicationServices")
    {
    }

    public DbSet<Tournament> tournaments { get; set; }
    public DbSet<Address> addresses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }

    public class Tournament
    {
        [ScaffoldColumn(false)]
        public int TournamentId { get; set; }


        [Required(ErrorMessage="Tournament name is a required field")]
        public string Name { get; set; }


        [Required(ErrorMessage="Address is a required field")]
        public Address Address { get; set; }
    }

    public class Address
    {   
        [ScaffoldColumn(false)]
        public int AddressId { get; set; }


        [Required(ErrorMessage="Street name is a required field")]
        public string Street { get; set; }


        [Required(ErrorMessage="House number is a required field")]
        public string HouseNo { get; set; }
    }
}

When I insert a new Tournament with an Address and I go check in the database if the relation is used I can see it worked. The tournament has an Address_AddressId value pointing to the new inserted Address. But when I try to get the info by doing this:

Tournament tournament = context.tournaments.Find(id);

and I debug it, I can see all the data from Tournament is in the tournament object, except for the Address. This is set to null and I have absolutely no idea why.

Can you guys help me out?

Thanks in advance,
Bart

  • 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-02T23:02:10+00:00Added an answer on June 2, 2026 at 11:02 pm

    You need to get familiar a bit how you can load related data with Entity Framework. An introduction is here: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx These are really basics you need to know to work efficiently with Entity Framework.

    The behaviour you are seeing is expected. Entity Framework doesn’t load a navigation property – like Tournament.Address – automatically when you just load the parent entity Tournament (with Find in your example).

    There are basically three options to load related data:

    • Eager loading:

      Tournament tournament = context.tournaments
          .Include(t => t.Address) // <- "eager loading"
          .SingleOrDefault(t => t.TournamentId == id);
      

      Tournament and address will we loaded in a single roundtrip and database query.

    • Lazy loading: Mark your navigation property as virtual:

      public virtual Address Address { get; set; }
      

      EF will dynamically create a proxy object (derived from Tournament) when you load the tournament which is capable to load the related entity as soon as you access one of its properties:

      Tournament tournament = context.tournaments.Find(id);
      string street = tournament.Address.Street; // second query to DB happens here
      
    • Explicit loading:

      Tournament tournament = context.tournaments.Find(id);
      context.Entry(tournament).Reference(t => t.Address).Load();
      // second query to DB happens here
      

      This is similar to lazy loading because you also need two queries and roundtrips to the database, but you control explicitly when the Address is loaded. Explicit loading has the option to filter or sort the related data (in case of navigation collections) which the other two options do not have.

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

Sidebar

Related Questions

This is my first time making a hash table. I'm trying to associate strings
This is first time I am doing a project in other then English Language
This is my first time building an iOS project using any kind of advanced
This is my first time attempting to call an ASP.NET page method from jQuery.
This project is literally making me lose sleep. I think about it all day
I am doing this first time so I need some help. I am using
This is my first time I ever created an air installer. When I clicked
This is my first time using joomla. I don't know if I'm using the
This is my first time with Web services. I have to develop web services
this is my first time posting here, I have a question which 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.