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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:18:06+00:00 2026-05-16T23:18:06+00:00

I have the following Entity Framework POCO classes: public class Customer { public int

  • 0

I have the following Entity Framework POCO classes:

public class Customer
{
    public int Id {get;set;}
    public string Name {get;set;}

    public virtual ICollection<Order> Orders {get;set;}
}

public class Order
{
    public int Id {get;set;} 
    public int CustomerId {get;set;}
    public int OrderTypeId {get;set;}

    public virtual OrderType Type {get;set;}
    public virtual Customer Customer {get;set;}
} 

public class OrderType 
{
    public int Id {get;set;}
    public virtual ICollection<Order> Orders {get;set;}
}

The problem is that when I return my ICollection<Order> I’m getting the Orders alright, but the OrderType property of the Order is not being populated. My Order will contain the following detail:

Id:          1
CustomerId:  1
Customer:    Populated
OrderTypeId: 3
Type:        null        // Never returned

My mapping code looks like the following:

public void ConfigureOrder(ModelBuilder builder)
{
    // Mapping from Order -> Customer
    builder.Entity<Order>()
        .HasRequired(x => x.Customer)
            .WithMany(x => x.Orders)
                .HasConstraint((order, cust) => order.CustomerId == cust.Id);

    // Mapping from Order -> OrderType
    builder.Entity<Order>()
        .HasRequired(x => x.OrderType)
            .WithMany(x => x.Orders)
                .HasConstraint((order, type) => order.OrderTypeId == type.Id);
}

I’ve then disabled Lazy loading in my context:

public Context(string connectionString)
    : base(connectionString)
{
    ObjectContext.ContextOptions.LazyLoadingEnabled = false;
}

So to return the data in my repository I use the Include method of System.Data.Entity:

var query = from item in context.Customers
                .Include(x=> x.Orders)
            where item.Id == customerId
            select item;

I was assuming that because I couldn’t specify Orders.OrderType, that was the problem, so I tried some variations:

1 -> .Include(x=> x.Orders.FirstOrDefault().OrderType)
2 -> .Include("Orders")
3 -> .Include("Orders")
     .Include("Orders.OrderType")

But I can never get the OrderType property to be returned, unless I just load the Order directly:

var query = from item in context.Orders
                .Include(x=> x.OrderType)
            select item;

This code will correctly return the OrderType within the order.

  • 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-16T23:18:07+00:00Added an answer on May 16, 2026 at 11:18 pm

    Oh dear. Looks like I was a total donkey. It’s 17:45, I should of gone home by now anyway.

    I had two Get methods:

    Get(int customerId)
    {
        // This was the method I was testing within
        var query = from item in context.Customers
                        .Include("Orders.OrderType")
                    select item;
    }
    
    Get(int customerId, int versionId)
    {
        // This was the method that was running
        var query = from item in context.Customers
                        .Include(item.Orders)
                    select item;
    }
    

    So, "Orders.OrderType" was the correct, albeit nasty looking solution. I need some caffeine.

    EDIT:

    Just coming back to this question, the best way for includes is to use System.Data.Entity‘s Include method:

    .Include(x=> x.Orders.Select(o=> o.OrderType));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entity: public class User { public int ID {get; set;}
I have the following entities in Entity Framework: public class School { public int
I have the following mapping: @Entity @Table(name = Prequalifications) public class Prequalification implements Serializable
If i have the following entity: public class PocoWithDates { public string PocoName {
I am using Entity Framework and have the following classes class Student { [Key]
I have the following classes generated by the Reverse engineering tool (Entity Framework Power
Trying to map the following schema using the Entity Framework. A Customer can have
I have the following entity with the following relation: @Entity @Table(name = bpr_user) public
I have the following entity class : @Entity @Table(name = THE_TREE, catalog = ,
Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long

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.