Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Cascade delete from two separate tables
    primarykey
    data
    text
    <p>I have the following two entities:</p> <pre><code>public class Field { public int FieldID { get; set; } public String Name { get; set; } public int LocationID { get; set; } public virtual ICollection&lt;FieldPlanning&gt; FieldPlannings { get; set; } } public class Timeslot { public int TimeslotID { get; set; } public DateTime Start { get; set; } public int MatchDayID { get; set; } public virtual ICollection&lt;FieldPlanning&gt; FieldPlannings { get; set; } } </code></pre> <p>Now a combination of these two entities makes up the following entity:</p> <pre><code>public class FieldPlanning { public int FieldPlanningID { get; set; } public int TimeslotID { get; set; } public int FieldID { get; set; } public virtual Timeslot Timeslot { get; set; } public virtual Field Field { get; set; } public virtual Match Match { get; set; } } </code></pre> <p>This entity would then also have a navigation property to the Match entity, but I've left this out for brevity.</p> <p>When either a Field or a Timeslot is deleted I want it to delete the associated FieldPlanning records as well.</p> <p>If I run the application I get the error that 'Timeslot_FieldPlanning may cause cycles or multiple cascade paths'. If I then edit the modelcreating like this:</p> <pre><code> modelBuilder.Entity&lt;Timeslot&gt;() .HasMany(ts =&gt; ts.FieldPlannings) .WithRequired(fp =&gt; fp.Timeslot) .HasForeignKey(fp =&gt; fp.TimeslotID) .WillCascadeOnDelete(false); </code></pre> <p>If I then delete a Field, the FieldPlanning is deleted with it without problem. If I try to delete a Timeslot, I get the same error as before.</p> <p>How can I fix it so that I can delete either of Field or Timeslot and for both Entities the CascadeOnDelete can be true?</p> <p>I read Hans Riesebos' answer <a href="https://stackoverflow.com/questions/851625/foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths">here</a> but I couldn't figure out how to apply it to my problem.</p> <p>EDIT: My Location Entity:</p> <pre><code>public class Location { public int LocationID { get; set; } public String Name { get; set; } public Address Address { get; set; } public int TournamentID { get; set; } public virtual Tournament Tournament { get; set; } public virtual ICollection&lt;Field&gt; Fields { get; set; } } </code></pre> <p>My Match Entity:</p> <pre><code>public class Match { public int MatchID { get; set; } public Boolean Forfeited { get; set; } public int TeamAID { get; set; } public int TeamBID { get; set; } public int FieldPlanningID { get; set; } public virtual Team TeamA { get; set; } public virtual Team TeamB { get; set; } public virtual FieldPlanning FieldPlanning { get; set; } public virtual ICollection&lt;Official&gt; Officials { get; set; } } </code></pre> <p>When trying it out, I specifically commented the navigation property to Match, so I could make sure it first worked without Match because I know I have problem in the relationship between FieldPlanning and Match, as this is a 0..1 to 0..1 relationship. But I don't see how this matters in my current problem (so long as I keep the navigation property of Match in FieldPlanning commented).</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.
 

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