title: How do you delete a single record from EF, when you're using generics?
body: <p>I'm having a problem in which the deletion of a record in a table works, but only so long as the table has no child foreign key relationships. Now, when we have a parent table and want to delete the record using EF, it fails with an error of, "The relationship could not be changed because one or more of the foreign-key properties is non-nullable." I've deleted cascading delete on the child table referencing the parent table. I've tested it within SSMS. It works fine. But from within C# it fails. We have a view model where we've defined a generic DeleteRow method. This works great, so long as the row being deleted has no child tables. It fails, if the table is a parent table. I tried looking here on Stackoverflow for an answer and came across this question, now closed: <a href="https://stackoverflow.com/questions/17723276/delete-a-single-record-from-entity-framework">stackoverflow.com/questions/17723276/delete-a-single-record-from-entity-framework</a>. The answer to this question used a discrete table in EF. We're using generics. Here's our code:</p>
  
  <pre><code>public void DeleteRow(T row)
  {
      if (App.MainDataContext != null &amp;&amp; row != null)
      {
          App.MainDataContext.Entry&lt;T&gt;(row).State = EntityState.Deleted;
          App.MainDataContext.SaveChanges();
      }
  }
  </code></pre>
  
  <p>I don't believe setting the State property to the enumeration EntityState.Deleted is working, as that's only setting it for the record in the parent table, not for any of the related records in the child tables. (MainDataContext is entity from a .EDMX file.) How do we delete a single record from any table using EF 6 when we're using generics to pass in the table type?</p>