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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:10:56+00:00 2026-05-23T16:10:56+00:00

I have classes that are structured like the following: public class Forecast { [Key]

  • 0

I have classes that are structured like the following:

public class Forecast
{
    [Key]
    [ForeignKey("Stop")]
    public string Abbreviation { get; set; }
    public virtual Stop Stop { get; set; }
    public virtual List<Direction> Directions { get; set; }
}

public class Direction
{
    public int DirectionId { get; set;}
    public string Abbreviation { get; set;}
    public virtual Forecast Forecast { get; set;}
    public virtual List<Transport> Transports { get; set;}
}

public class Transport
{
    public int TransportId { get; set; }
    public int DirectionId { get; set;}
    public virtual Direction Direction { get; set;}
}

public partial class Stop
{
    [Key]
    public string Abbreviation { get; set; }
    public virtual Forecast Forecast { get; set; }
}

I developed these classes and used EF Code First 4.1 to generate the database. CF does appear to properly create all of the primary and foreign key relationships between the classes within the database (MSSQL).

My problem is when I want to delete a Forecast. I thought I do could something like the following:

        using (MyContext ctxt = new MyContext())
        {
            // get a forecast, somehow, not really important
            // The one assumption is I'm absolutely sure it's 
            // Abbreviation key already exists in the database
            // and the list of Forecasts.
            Forecast f;

            ctxt.Forecasts.Remove(f);
        }

This deletes the top-level object from the database just fine. However, all of its child objects – all of the directions and transports – remain in the database and become orphaned (their key relationship column gets set to null. I expect that but I DON’T know why they’re not just deleted). I have resorted to recursing down the object graph and calling Remove on every object from its appropriate DbSet in ctxt, but that seems like… the wrong way to do it.

What am I missing here?

Why can’t I just say

ctxt.Forecasts.Remove(f);

and be done with it?

Edit:

@Ladislav gave me the right answer – I
needed to add [Required] to the
Abbreviation property on Direction.

However, I am still forced to actually
load the child entities for this to
work – doing something as simple as

Direction d = f.Directions[0];

will cause the delete to actually
delete the child entities. I’m well
aware that this is due to lazy
loading. I thought the point of the
FK relationship and ON CASCADE DELETE
was that you wouldn’t have to actually
load the entities to delete them?

Again I seem to be missing something simple.

  • 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-23T16:10:57+00:00Added an answer on May 23, 2026 at 4:10 pm

    @Eranga is right that this is done by ON DELETE CASCADE setting on relation in the database BUT you are using code first approach and EF creates database for you so the problem here is that your model is not correctly defined because EF didn’t create cascading rule for you.

    Why? Because of this:

    public class Direction
    {
        public int DirectionId { get; set; }
        public string Abbreviation { get; set; }
        public virtual Forecast Forecast { get; set; }
        public virtual List<Transport> Transports { get; set; }
    }
    

    Abbreviation is FK property and it is nullable! So EF looks at your model and it sees that you defined Direction entity which can have Abbreviation set to null and because of that it can exists orphaned. Change it to:

    public class Direction
    {
        public int DirectionId { get; set; }
        [Required]
        public string Abbreviation { get; set; }
        public virtual Forecast Forecast { get; set; }
        public virtual List<Transport> Transports { get; set; }
    }
    

    and removing Forecast will delete all related Direction instances and Transport instances. Stop is different story because it is parent entity to Forecast so it will never be removed with Forecast.

    Edit:

    One more point – you don’t want to add ON DELETE CASCADE to your relations manually because EF have to know about enabled cascade deletes. EF use this information in case where you have related entities loaded.

    If you place the rule manually into the database you must use fluent mapping and tell EF about this rule as well. Once you force cascade delete in fluent api you don’t need to make it manually in the database – it will be created automatically during database recreation.

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

Sidebar

Related Questions

I have a set of core, complicated JavaScript data structures/classes that I'd like to
I have classes that store data, and methods to go get data for individual
I have multiple classes that all derive from a base class, now some of
I have data that has been stored using binary serialization for the following class:
I have a few classes, such as those that outline database table structure or
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
I have two classes that each need an instance of each other to function.
I have some classes that will do something based on some conditions . The
I have several classes that conceptually belong to one tier. They have no common

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.