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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:52:58+00:00 2026-05-16T00:52:58+00:00

First some brief background: I have an existing ASP.NET MVC 1 application using Entity

  • 0

First some brief background: I have an existing ASP.NET MVC 1 application using Entity Framework v1 which works fairly well, though because there are getting on to 40 tables the .edmx is getting unwieldy and prone to corruptions with the Visual Studio 2008 designer.
What I want to do is to see if it’s feasible to migrate the DAL to use EF4 and Code-First.

Initially I’m trying to model simple parent/child relationships but not getting very far. I have 2 tables Client and Address which correspond to the following POCO classes:

public class Client
{
    public int ClientId { get; set; }
    public string Name { get; set; }
    public Address HomeAddress { get; set; }
    public Address WorkAddress { get; set; }
    // More properties here
}

public class Address
{
    public int AddressId { get; set; }
    public string NameOrNumber { get; set; }
    public string Line1 { get; set; }
    // More properties here
}

In addition I have a DbContext class in which I define the relationships using the fluent api:

public class AppContext : DbContext
{
    public SlobContext() : base()
    {
        this.ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }

    public DbSet<Client> Clients
    {
        get;
        set;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().MapSingleType().ToTable("Address");
        modelBuilder.Entity<Client>().HasKey(c => c.ClientID);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.HomeAddress);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.WorkAddress);

        modelBuilder.Entity<Client>()
            .MapSingleType(c => new
            {
                Name = c.Name,
                ClientID = c.ClientID,
                HomeAddressID = c.HomeAddress.AddressID,
                WorkAddressID = c.WorkAddress.AddressID
            })
            .ToTable("Client");
    }
}

So then in my Controller I can return the following as my Model:

Context.Clients.Take(10).OrderBy(i => i.Name)

Which returns 10 results from the database as expected, apart from that it returns a default Address object for Client.WorkAddress and Client.HomeAddress.

My guess is that either I’m setting ObjectContext.ContextOptions.LazyLoadingEnabled = true in the wrong place, or (more likely) I’m not getting my relationships between Client and Address correct.

  • 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-05-16T00:52:58+00:00Added an answer on May 16, 2026 at 12:52 am

    Your address properties aren’t virtual. Lazy loading can’t work on a POCO unless the properties are virtual.

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

Sidebar

Related Questions

First a brief background. I am using .NET output caching and substitution controls to
First some background, I've written an open source .NET library, named Duplicitiy (on github.com),
First some background: I'm working on an application and I'm trying to follow MVVM
Hi and thanks for your attention. First some background on the question: I have
We have an ASP.NET page which uses an update panel for partial page postbacks.
First Some Background I'm planning out the architecture for a new PHP web application
OK, so first some things that might be relevant: I'm using the Clang 3.1
Today i have a little problem containing a File Upload. First some Infos: Rubyversion:
I have some coding practices involving delegates that I'm not sure of. First of
I have gone thru some of the links for hibernate fetch strategies on net.One

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.