Note that there are some explanatory texts on larger screens.

plurals
  1. PODeletion of entire entity graph, including relationships, using EF Code First
    primarykey
    data
    text
    <p>I have classes that are structured like the following:</p> <pre><code>public class Forecast { [Key] [ForeignKey("Stop")] public string Abbreviation { get; set; } public virtual Stop Stop { get; set; } public virtual List&lt;Direction&gt; 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&lt;Transport&gt; 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; } } </code></pre> <p>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).</p> <p>My problem is when I want to delete a Forecast. I thought I do could something like the following:</p> <pre><code> 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); } </code></pre> <p>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.</p> <p>What am I missing here?</p> <p>Why can't I just say</p> <p>ctxt.Forecasts.Remove(f);</p> <p>and be done with it?</p> <p>Edit:</p> <blockquote> <p>@Ladislav gave me the right answer - I needed to add [Required] to the Abbreviation property on Direction.</p> <p>However, I am still forced to actually load the child entities for this to work - doing something as simple as</p> <p>Direction d = f.Directions[0];</p> <p>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?</p> </blockquote> <p>Again I seem to be missing something simple.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload