title: c# - Entity Framework Core Cascade Delete on One-To-One Relationship
body: <p>I have a one-to-one relationship between Service and Lexikon while the Service has a foreign key to Lexikon. My Service doesn't need a Lexikon so I made the LexikonID nullable. But my Lexikon can only exist if it is related to a Service so I made the navigation property [required]. I'm using Code First so EF builds my Database.</p>
  
  <pre><code> public class Service
   {
  
      public int ServiceID { get; set; }
  
      //Foreignkey
      public int? LexikonID { get; set; }
  
      //Navigation Properties
  
      public Lexikon Lexikon { get; set; }
   }
  
   public class Lexikon
   {
      public int LexikonID { get; set; }
  
      //Navigation Properties
      [Required]
      public SoftwareService SoftwareService { get; set; }
  
   }
  </code></pre>
  
  <p>I'm trying to delete the data automatically in table Lexikon if i delete my Service but unfortunately I'm not sure how to do this since I'm new to EF. </p>
  
  <p>I tried this code in dbContext:</p>
  
  <pre><code>       modelBuilder.Entity&lt;SoftwareService&gt;()
                  .HasOne(l =&gt; l.Lexikon)
                  .WithOne(s =&gt; s.SoftwareService)
                  .HasForeignKey&lt;SoftwareService&gt;(l =&gt; l.LexikonID)
                  .OnDelete(DeleteBehavior.Cascade);
  </code></pre>
  
  <p>but when I delete my Service the data in table Lexikon will still be in it. On the other hand when I delete the lexikon it will automatically delete the Service which is nice but I also want it to be happen to my Lexikon when I delete my Service.</p>
  
  <p>Unfortunately I'm not familiar with the OnDelete-Method so I hope someone can help me.</p>