title: Checking for Referential Integrity Violation before deletion in ASP .NET MVC
body: <p>I have an object <code>Tag</code> that is linked to a whole bunch of other objects.  I am attempting to handle the <em>Delete</em> for this object.  I simply don't want to delete the tag if it is associated to any other objects.  </p>
  
  <p>At the moment I am solving it like this:</p>
  
  <pre><code>if(tag.Stands.Count == 0 &amp;&amp; tag.Skids.Count == 0
     &amp;&amp; tag.Panels.Count == 0 &amp;&amp; tag.Devices.Count == 0
     &amp;&amp; tag.Cables.Count == 0 &amp;&amp; tag.JunctionBoxes.Count == 0)
  {
      _db.Tags.DeleteObject(tag);
      _db.SaveChanges();
  }
  </code></pre>
  
  <p>If I don't check for all these things, I obviously get a reference constraint error, e.g.</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: The DELETE statement conflicted
    with the REFERENCE constraint "FKStandTag237968".</p>
  </blockquote>
  
  <p>I know a way to get around this is with cascading deletes but it is actual business logic that tags shouldn't be deleted if they are associated to any other object.</p>
  
  <p>Is there a way using the Entity Framework that I can check that I won't break any constraints on the DB before attempting to save a delete?  Something along the lines of <code>_db.IsValidToDelete(object)</code>?</p>