Delete association

4 posts, 0 answers
  1. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 10 Mar 2011 Link to this post

    Hi,

    I've created a Domain Model from my database. I'm having troubles with my many-to-many relations...

    I have the following entities:
    - Product
    - Keyword

    There is an association between them:
    - ProductsReferenceKeywords (ZeroMany, ZeroMany)

    So, there is a Join table in my database. Inserting records is okay, but deleting records won't work.
    I've switched on the 'Managed' option on the Product entity.

    In SQL Server there are relationships between the tables and also CASCADE UPDATE / DELETE is switched on.

    This is the code for deleting an association:

    /// <summary>
            /// Delete a keyword from a product
            /// </summary>
            /// <param name="keywordid"></param>
            /// <param name="productid"></param>
            /// <returns></returns>
            public bool DeleteKeywordFromProduct(Guid keywordid, Guid productid) {
      
                bool result = false;
                try
                {
                    Product p = context.Products.Where(x => x.ProductId == productid).FirstOrDefault();
                    Keyword k = context.Keywords.Where(x => x.KeywordId == keywordid).FirstOrDefault();
      
                    p.Keywords.Remove(k);
                    context.SaveChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return result;
            }

    Nothing happens with this solution.

    Anyone has an idea?
    Also tried to remove the Product object from the Keyword collection...

    Regards,
    Daniel
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 15 Mar 2011 Link to this post

    Hello Daniel,

    In order to delete a Product or Keyword object from the database you need to call context.Delete(object) where object is the Product or Keyword instance. This would physically delete the record and also the relation to the other record. The code you currently have is only deleting the relation between the objects (the row from the join table) but not the record itself. I hope that helps.

    Greetings,
    Alexander
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft banner
  4. Daniel Plomp
    Daniel Plomp avatar
    7 posts
    Member since:
    Jun 2012

    Posted 16 Mar 2011 Link to this post

    Yes, that is also what I want to achieve. Deleting the record from the Join table. But with the code above, that is not happening? Daniel
  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 17 Mar 2011 Link to this post

    Hello Daniel,

    Firstly I would like to make a short note that there was a problem with the Managed option in the last few builds of the product and even if this option was enabled, the behavior was not changed. This is already fixed in the new Q1 release.

    I just verified (with the 2010.3.1125 version) that deleting records from the join table is possible in exactly the same way as you have tried. However, it works only in one direction due to the problem I mentioned above. So please try to find again which is the correct direction in your case - p.Keywords.Remove(k) or k.Products.Remove(p). You should also ensure that the object you are trying to remove from the collection is actually contained there. Maybe you could modify the second linq query to something like this:
    Keyword k = context.Keywords.Where(x => x.KeywordId == keywordid && p.Keywords.Contains(x)).FirstOrDefault();
    Please let me know if this does not help.

    Regards,
    Alexander
    the Telerik team
Back to Top