title: Entity Framework DELETE statement conflicted with the REFERENCE constraint
body: <p>I’m pretty new to EF and I have a little problem.</p>
  
  <p>I just want to delete an item in my database. I’m using SQL Server 2012 Express, VS2012, AdventureWorks 2012. </p>
  
  <p>The query that I execute is the following:</p>
  
  <pre><code>context = new AWEntities();
              var removedItem = context.Addresses
                  .Include("StateProvince")
                  .Include("SalesOrderHeaders")
                  .Include("BusinessEntityAddresses").Single(d =&gt; d.AddressID == 11);
              context.Addresses.Remove(removedItem);
  
  context.SaveChanges();
  </code></pre>
  
  <p>The error that I get is</p>
  
  <blockquote>
    <p>The DELETE statement conflicted with the REFERENCE constraint "FK_SalesOrderHeader_Address_ShipToAddressID". The conflict occurred in database "AdventureWorks2012", table "Sales.SalesOrderHeader", column 'ShipToAddressID'.
        The statement has been terminated.</p>
  </blockquote>
  
  <p>Is this actually a good way to delete items and the according entries in the other tables?</p>
  
  <p>Please point me into the right direction.</p>
  
  <pre><code>   public partial class Address
      {
          public Address()
          {
              this.BusinessEntityAddresses = new HashSet&lt;BusinessEntityAddress&gt;();
              this.SalesOrderHeaders = new HashSet&lt;SalesOrderHeader&gt;();
          }
  
          public int AddressID { get; set; }
          public string AddressLine1 { get; set; }
          public string AddressLine2 { get; set; }
          public string City { get; set; }
          public int StateProvinceID { get; set; }
          public string PostalCode { get; set; }
          public System.Data.Spatial.DbGeography SpatialLocation { get; set; }
          public System.Guid rowguid { get; set; }
          public System.DateTime ModifiedDate { get; set; }
  
          public virtual StateProvince StateProvince { get; set; }
          public virtual ICollection&lt;BusinessEntityAddress&gt; BusinessEntityAddresses { get; set; }
          public virtual ICollection&lt;SalesOrderHeader&gt; SalesOrderHeaders { get; set; }
      }
  
  
  public partial class StateProvince
      {
          public StateProvince()
          {
              this.Addresses = new HashSet&lt;Address&gt;();
              this.SalesTaxRates = new HashSet&lt;SalesTaxRate&gt;();
          }
  
          public int StateProvinceID { get; set; }
          public string StateProvinceCode { get; set; }
          public string CountryRegionCode { get; set; }
          public bool IsOnlyStateProvinceFlag { get; set; }
          public string Name { get; set; }
          public int TerritoryID { get; set; }
          public System.Guid rowguid { get; set; }
          public System.DateTime ModifiedDate { get; set; }
  
          public virtual ICollection&lt;Address&gt; Addresses { get; set; }
          public virtual CountryRegion CountryRegion { get; set; }
          public virtual ICollection&lt;SalesTaxRate&gt; SalesTaxRates { get; set; }
          public virtual SalesTerritory SalesTerritory { get; set; }
      }
  }
  
  public partial class BusinessEntityAddress
  {
      public int BusinessEntityID { get; set; }
      public int AddressID { get; set; }
      public int AddressTypeID { get; set; }
      public System.Guid rowguid { get; set; }
      public System.DateTime ModifiedDate { get; set; }
  
      public virtual Address Address { get; set; }
      public virtual AddressType AddressType { get; set; }
      public virtual BusinessEntity BusinessEntity { get; set; }
  }
  
  
  public partial class SalesOrderHeader
      {
          public SalesOrderHeader()
          {
              this.SalesOrderDetails = new HashSet&lt;SalesOrderDetail&gt;();
              this.SalesOrderHeaderSalesReasons = new HashSet&lt;SalesOrderHeaderSalesReason&gt;();
          }
  
          public int SalesOrderID { get; set; }
          public byte RevisionNumber { get; set; }
          public System.DateTime OrderDate { get; set; }
          public System.DateTime DueDate { get; set; }
          public Nullable&lt;System.DateTime&gt; ShipDate { get; set; }
          public byte Status { get; set; }
          public bool OnlineOrderFlag { get; set; }
          public string SalesOrderNumber { get; set; }
          public string PurchaseOrderNumber { get; set; }
          public string AccountNumber { get; set; }
          public int CustomerID { get; set; }
          public Nullable&lt;int&gt; SalesPersonID { get; set; }
          public Nullable&lt;int&gt; TerritoryID { get; set; }
          public int BillToAddressID { get; set; }
          public int ShipToAddressID { get; set; }
          public int ShipMethodID { get; set; }
          public Nullable&lt;int&gt; CreditCardID { get; set; }
          public string CreditCardApprovalCode { get; set; }
          public Nullable&lt;int&gt; CurrencyRateID { get; set; }
          public decimal SubTotal { get; set; }
          public decimal TaxAmt { get; set; }
          public decimal Freight { get; set; }
          public decimal TotalDue { get; set; }
          public string Comment { get; set; }
          public System.Guid rowguid { get; set; }
          public System.DateTime ModifiedDate { get; set; }
  
          public virtual Address Address { get; set; }
          public virtual ShipMethod ShipMethod { get; set; }
          public virtual CreditCard CreditCard { get; set; }
          public virtual CurrencyRate CurrencyRate { get; set; }
          public virtual Customer Customer { get; set; }
          public virtual ICollection&lt;SalesOrderDetail&gt; SalesOrderDetails { get; set; }
          public virtual SalesPerson SalesPerson { get; set; }
          public virtual SalesTerritory SalesTerritory { get; set; }
          public virtual ICollection&lt;SalesOrderHeaderSalesReason&gt; SalesOrderHeaderSalesReasons { get; set; }
      }
  </code></pre>