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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:41:24+00:00 2026-06-14T09:41:24+00:00

Getting what I think is an odd behaviour in EF, which I’m hoping someone

  • 0

Getting what I think is an odd behaviour in EF, which I’m hoping someone can shed some light on. Basicaly, if I retrieve an item with a foreign key, the foreign key item isn’t retrieved? This seems like a bit of a short coming. Have I missed something obvious or is there a pattern to deal with this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace EFTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new MCInitializer());

            EFTestEM context = new EFTestEM();
            var foos = from m in context.Foo
                    select m;

            foreach (var foo in foos)
            {
                // foo.MyBar is null?! How do I populate it?
                Console.WriteLine("{0},{1}",foo.Desc,foo.MyBar.Whatever);
            }

        }

    }


    [Table("tbl_Bar")]
    public class Bar
    {
        [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int BarId { get; set; }
        public string Whatever { get; set; }
        public string Whenever { get; set; }
    }


    [Table("tbl_Foo")]
    public class Foo
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int FooId { get; set; }
        public string Desc { get; set; }
        public int MyBarId { get; set; }
        [ForeignKey("MyBarId")]
        public Bar MyBar { get; set; }

    }

    public class MCInitializer : DropCreateDatabaseAlways<EFTestEM>
    {
        protected override void Seed(EFTestEM context)
        {
            List<Bar> bars = new List<Bar>
                             {
                                 new Bar(){Whatever = "Bar1"},
                                 new Bar(){Whatever = "Bar2"},
                                 new Bar(){Whatever = "Bar3"},
                             };


            List<Foo> foos = new List<Foo>
                             {
                                 new Foo() {Desc = "Foo1", MyBar = bars[0]},
                                 new Foo() {Desc = "Foo2", MyBar = bars[1]},
                                 new Foo() {Desc = "Foo3", MyBar = bars[2]}
                             };

            foreach (var bar in bars)
                context.Bar.Add(bar);
            foreach (var foo in foos)
                context.Foo.Add(foo);

            context.SaveChanges();

            base.Seed(context);

        }
    }
}
  • 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-14T09:41:25+00:00Added an answer on June 14, 2026 at 9:41 am

    For lazy loading you need to make your related properties virtual.

    E.g.

    public virtual Bar MyBar { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I might be missing something, but I'm getting what I think seems an odd
I think I'm getting senile because I was convinced that to give a name
I think I'm getting closer to understanding how to move my site towards a
Ok I think I'm getting the previous year instead of the previous day, but
Just when I think I'm getting an understanding of the syntax of Javascript, I
I think something buggy is up with my GET method as I am getting
I keep getting a exc bad access error and I think it has something
I am getting to know boost::variant. I think this example should work. #include <boost/fusion/sequence.hpp>
Hey guys I'm getting an error right around my second nested else, I think
I am getting a NullReferenceException when I run the following program. I think the

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.