title: Remove an item from Collection using Entity Framework
body: <p>I am using DDD. I have a class Product which is an aggregate root.</p>
  
  <pre><code>public class Product : IAggregateRoot
  {
      public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  
      public void AddComment(Comment comment)
      {
          Comments.Add(comment);
      }
  
      public void DeleteComment(Comment comment)
      {
          Comments.Remove(comment);
      }
  }
  </code></pre>
  
  <p>The layer which holds the models doesn't know about EF at all. The problem is that when i call <code>DeleteComment(comment)</code>, EF throws exception</p>
  
  <p>A relationship from the 'Product_Comments' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Product_Comments_Target' must also in the 'Deleted' state.</p>
  
  <p>Even if the element is removed from the collection, EF doesn't delete it. What should i do to fix this without breaking DDD? (I am thinking of making a repository for Comments as well, but is not right)</p>
  
  <p><strong>Code example:</strong></p>
  
  <p>Because i am trying to use DDD, the <code>Product</code> is an aggregate root, and it has a repository <code>IProductRepository</code>. A Comment cannot exists without a product, therefore is a children of <code>Product</code> Aggregate, and <code>Product</code> is responsible for creating and deleting Comments. <code>Comment</code> does not have a Repository.</p>
  
  <pre><code>public class ProductService
  {
      public void AddComment(Guid productId, string comment)
      {
          Product product = _productsRepository.First(p =&gt; p.Id == productId);
          product.AddComment(new Comment(comment));
      }
  
      public void RemoveComment(Guid productId, Guid commentId)
      {
          Product product = _productsRepository.First(p =&gt; p.Id == productId);
          Comment comment = product.Comments.First(p =&gt; p.Id == commentId);
          product.DeleteComment(comment);
  
  
          // Here i get the error. I am deleting the comment from Product Comments Collection,
          // but the comment does not have the 'Deleted' state for Entity Framework to delete it
  
          // However, i can't change the state of the Comment object to 'Deleted' because
          // the Domain Layer does not have any references to Entity Framework (and it shouldn't)
  
          _uow.Commit(); // UnitOfWork commit method
  
      }
  }
  </code></pre>