1

I am trying to delete a row from the database which has a split table assoicated with it, however, I cannot seem to delete the split table relationship.

Here is the code I am trying to execute

  public void DeleteProductItemSplit(ProductItem pi)
  {
     // ProductItemData is the split table
     var data = new ProductItemData() { Id = pi.Id };

     m_Context.ProductItemData.Attach(data);

     m_Context.ProductItemData.Remove(data);
  }

On the second line

     m_Context.ProductItemData.Attach(data);

I get this error

Additional information: Multiplicity constraint violated. The role 'ProductItem_Data_Target' of the relationship 'UpdaterDataLayer.ProductItem_Data' has multiplicity 1 or 0..1.

If I ignore the attach line, then I get this error when trying to remove the object

The object cannot be deleted because it was not found in the ObjectStateManager.

Any ideas on how to solve?

Thanks

user3428422
  • 4,300
  • 12
  • 55
  • 119

1 Answers1

0

If you want to delete it just use this:

public void DeleteProductItemSplit(ProductItem pi)
{
    m_Context.ProductItemData.Remove(m_Context.ProductItemData.Find(pi.Id));
}

Find allows you to easily get element by primary key. The on resulting entity you may invoke Remove to finally delete it.

mr100
  • 4,340
  • 2
  • 26
  • 38