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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:24:12+00:00 2026-06-09T17:24:12+00:00

I am trying to delete some data (how hard can it be, anyway?) but

  • 0

I am trying to delete some data (how hard can it be, anyway?) but whenever I remove a complex-type prop from my model object, the contents of an ICollection prop are also mixed. Here’s the code:

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(string name)
    {
        using (var db = new HoursDb())
        {
            db.Configuration.ProxyCreationEnabled = false;

            var schedule = db.Schedules.Where(s => s.ScheduleId == name)
                .Include(s => s.WeekSpec)
                .Include(s => s.WeekSpec.DaySpecs)
                .Include(s => s.ExceptionHoursSets)
                .Include(s => s.ExceptionHoursSets.Select(e => e.WeekSpec))
                .Include(s => s.ExceptionHoursSets.Select(e => e.WeekSpec.DaySpecs))
                .FirstOrDefault();
            if (schedule == null)
            {
                return View("404");
            }


            var daySpecs = new List<DaySpec>(schedule.WeekSpec.DaySpecs);
            foreach (var daySpec in daySpecs)
            {
                db.DaySpecs.Remove(daySpec);
            }
            db.WeekSpecs.Remove(schedule.WeekSpec);

            var exceptionHoursSets = new List<ExceptionHoursSet>(schedule.ExceptionHoursSets);
            foreach (var exceptionHoursSet in exceptionHoursSets)
            {
                daySpecs = new List<DaySpec>(exceptionHoursSet.WeekSpec.DaySpecs);
                foreach (var daySpec in daySpecs)
                {
                    db.DaySpecs.Remove(daySpec);
                    db.SaveChanges();
                }
                db.WeekSpecs.Remove(exceptionHoursSet.WeekSpec);
                db.SaveChanges();
                db.ExceptionHoursSets.Remove(exceptionHoursSet);
            }

            db.Schedules.Remove(schedule);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
    }

Before I fire off the line with

db.WeekSpecs.Remove(schedule.WeekSpec);

ExceptionHoursSets has, say, three items. After removing the WeekSpec, it has zero. Eh. Any ideas?

Update

Here’s the code for the HoursDb class:

using System.Data.Entity;
using LibraryAdmin.Models;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ####.DAL
{
    public class HoursDb : DbContext
    {
        public DbSet<DaySpec> DaySpecs { get; set; }
        public DbSet<WeekSpec> WeekSpecs { get; set; }
        public DbSet<ExceptionHoursSet> ExceptionHoursSets { get; set; }
        public DbSet<Schedule> Schedules { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

Also, should it help, DaySpecs contain open and close hours for a generic building; they are contained in WeekSpecs.
The Schedule contains a WeekSpec to describe the general open hours, as well as a set of ExceptionHoursSets.
These have WeekSpecs describing exceptions to the aforementioned general hours.

  • 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-09T17:24:13+00:00Added an answer on June 9, 2026 at 5:24 pm

    Moving the foreach that deletes the ExceptionHoursSets to the front after the var schedule is initialized, as well as removing all but the last db.SaveChanges, seems to work. I still do not know why it does and what I had doesn’t, though, so any additional information is appreciated.

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

Sidebar

Related Questions

I'm trying to use MongoDB to cache some data for me, but I can't
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
Following on from a previous question, I'm trying to clean up some data where
I am trying to get some data from my database. This is my code
I am trying to get some data from page to page and then mail
Trying to archive some data from one database table to another over linked server
I am trying to write some SQL to automatically delete some records from the
So I am trying to Delete some items from the database before I Insert
I am trying to delete a string from a string, but the result of
I am trying to take a single input from the user and delete some

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.