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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:14:03+00:00 2026-06-11T18:14:03+00:00

I’ve got an EntityFramework based database here created by code first approach. The database

  • 0

I’ve got an EntityFramework based database here created by code first approach. The database contains two tables with a parent-child-relationship between them:

  1. Orderlists (based on model “Orderlist”)
  2. Orders (based on model “Order”)

where Orderlists contains an ICollection<Order> Orders which should (!) list all related orders. For some reason it doesn’t.

The DbContext of the controller contains two DbSets:

  1. DbSet<Orderlist>
  2. DbSet<Order>

The model Order on the other hand has got two properties (with its type in brackets):

  1. int OrderlistId
  2. Orderlist OrderList

In the controller in a [HttpPost] Create method for a new Order object I’m wiring up this relation by picking the appropriate Orderlist object (with the correctly provided/selected ID) from the DbContext, setting OrderlistId and Orderlist of the newly created Order object appropriately and adding the Order to the Orders collection of the picked Orderlist (creating the collection if it is null).

Then I do a db.SaveChages(); which does what it’s supposed to do.

When the database is generated at first from the code of the models, regarding the relationship only the OrderlistId property generates a field in the datatables. Neither the Orders collection of the Orderlist nor the Orderlist property of the Order generate fields in the tables. That’s okay since it is still a valid relation: the ID of the Orderlist is a PK and the OrderlistId value is set correctly. The schemes of the datatables also reflect the relation by foreign keys.

Now, the problem is that the relation isn’t restored when processing the data at a different point in my application. The Orders ICollection is always null when I pick it from the DbContext.

My question is: should the Orders ICollection be filled automatically when creating the DbContext object? Or do I have to fill it manually by iterating through the Orders DbSet of the DbContext and add every appropriate Order manually to its related Orderlist? And, if I have to, where and how do I have to do that?

  • 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-11T18:14:04+00:00Added an answer on June 11, 2026 at 6:14 pm

    All association properties in Entity Framework Code First models must be marked with the virtual keyword, if you want Entity Framework to automatically load the associated entities on-demand (lazy-loading).

    The following (basic) model definitions should work.

    public class OrderList
    {
        public int OrderListId { get; set; }
    
        public virtual ICollection<Order> Orders { get; set; } // note the use of virtual
    }
    
    public class Order
    {
        public int OrderId { get; set; }
    
        public int OrderListId { get; set; }
        public virtual OrderList OrderList { get; set; } // note the use of virtual
    }
    

    Also, note that when adding an Order to an OrderList, you don’t have to set the association properties on the Order entity. You can simply add the the new Order to the Orders collection on the OrderList entity, like this:

    int orderListId = 1;
    OrderList orderList = context.OrderLists.Find(orderListId);
    
    Order newOrder = new Order();
    orderList.Orders.Add(newOrder);
    context.SaveChanges();
    

    Alternatively, you could just set the OrderListId property on the new Order, and add it to the DbContext Orders collection.

    Order order = new Order();
    order.OrderListId = 1;
    context.Orders.Add(order);
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to select an H1 element which is the second-child in its group

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.