This is a migrated thread and some comments may be shown as answers.

Delete association

3 Answers 80 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel Plomp
Top achievements
Rank 2
Daniel Plomp asked on 10 Mar 2011, 11:01 AM
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

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 15 Mar 2011, 10:42 PM
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!
0
Daniel Plomp
Top achievements
Rank 2
answered on 16 Mar 2011, 09:34 AM
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
0
Alexander
Telerik team
answered on 17 Mar 2011, 02:42 PM
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
Tags
General Discussions
Asked by
Daniel Plomp
Top achievements
Rank 2
Answers by
Alexander
Telerik team
Daniel Plomp
Top achievements
Rank 2
Share this question
or