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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:23:51+00:00 2026-06-09T01:23:51+00:00

I am making an MCV open-hours admin, in which I have a class Schedule

  • 0

I am making an MCV open-hours admin, in which I have a class Schedule containing an ICollection of ExceptionHoursSets, each HoursSet further containing a WeekSpec. (The ExceptionHoursSets contain hours which define exceptions to a general WeekSpec hours pattern also contained in Schedule.)

Schedule.cs (abbreviated):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace HoursAdmin.Models
{
    public class Schedule
    {
        [Required]
        public Guid ScheduleId { get; set; }

        // Miscellaneous props

        // General hours pattern
        public Guid WeekSpecId { get; set; }
        public virtual WeekSpec WeekSpec { get; set; }

        // All exceptions to that pattern
        virtual public ICollection<ExceptionHoursSet> ExceptionHoursSets { get; set; }
    }
}

ExceptionHoursSet.cs (also abbreviated):

using System;
using System.ComponentModel.DataAnnotations;

namespace HoursAdmin.Models
{
    public class ExceptionHoursSet
    {
        [Required]
        public Guid ExceptionHoursSetId { get; set; }

        // More misc props

        public Guid WeekSpecId { get; set; }
        [Required]
        public WeekSpec WeekSpec { get; set; }
    }
}

WeekSpec.cs (abbreviated still):

using System;
using System.ComponentModel.DataAnnotations;

namespace HoursAdmin.Models
{
    public class DaySpec
    {
        [Required]
        public Guid DaySpecId { get; set; }

        // Good old misc props
    }
}

If I retrieve the Schedule, the HoursSet collection loads, but each HoursSet’s WeekSpec is null. I am currently disposed to ignore the nagging sense that I should use only Code First loading, and manually query for the WeekSpec whose ID matches that stored as the ExceptionHoursSet’s foreign key:

public ViewResult Index()
{
    using (var db = new HoursDb())
    {
        var schedules = db.Schedules.ToList();
        foreach (var schedule in schedules)
        {
            var exceptionHoursSets = schedule.ExceptionHoursSets;
            foreach (var exceptionHoursSet in exceptionHoursSets)
            {
                var weekSpec = db.WeekSpecs.FirstOrDefault(d => d.WeekSpecId == 
                    exceptionHoursSet.WeekSpecId);
                exceptionHoursSet.WeekSpec = weekSpec;
                db.Entry(weekSpec).Collection(w => w.DaySpecs).Load();
            }
        }
        return View(schedules);
    }
}

However, this is repetitive and tedious… so would anyone mind furnishing how this should be done?

P.S. — The answer supplied in Auto-retrieve ICollection of complex type with Code First won’t work, because as you can see I cannot put a nav prop in the WeekSpec to its parent entity, since that entity may either be a Schedule or an ExceptionHoursSet (see How to define an MVC database structure using the same sub-table in different super-tables).

Much appreciated,
Nathan Bond

  • 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-09T01:23:53+00:00Added an answer on June 9, 2026 at 1:23 am
     var schedules = db.Schedules
        .Include(s => s.WeekSpec)
        .Include(s => s.ExceptionHoursSets)
        .Include(s => s.ExceptionHoursSets.Select(e => e.WeekSpec))
        .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Making a simple program which will generate a multiple choice form. I have an
I'm experimenting with MCV using jquery. I'm making a call to an api, which
Making a word document of our network set-up. We have about 7 servers and
Making an adobe flex ui in which data that is calculated must use proprietary
Making a mobile friendly site, I have a single field and a submit button.
Does making a class field volatile prevent all memory visibility issues with it in
Making a small WCF test program which is based on a Store that has
making a multi-language site with codeginiter. I have created two folders. One for french
Making a new class in VS10 gives me using System; using System.Collections.Generic; using System.Linq;
Im making a class that is supposed to be able to assign c-strings 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.