views:

45

answers:

2

I have an EF4 model over a SQL Server 2008 db. In my model I have a many to many relationship:

Articles * - * Comments

and

Projects * - * Comments

In my Model, I simply created an association and set it up to many to many. In my db, I then get two additional tables ArticleComments and ProjectComments, which only hold the primarykeys to each table.

Problem is that when I delete a comment from an Article in my code (using article.Comments.Remove([some comment entity]) it only deletes the row from ArticleComments, and not the actual Comment it self.

Any solutions to this?

I've thought about setting it up as a trigger ondelete in the ProjectComments-table, but I feel this should be possible using only the Entity Framework 4 stuff.

A: 

Try to set on delete action to cascade. It's in association properties.

Yury Tarabanko
It's saying I can't: cannot have operation specified since its multiplicity is '*'. Operations cannot be specified on ends with multiplicity '*'.
Yngve B. Nilsen
Oh, I see... Missed that point.
Yury Tarabanko
+2  A: 

What you did only removes the association between an article and a comment (does not delete data from Comments table but from ArticleComments).

For deleting a Comment you must call the DeleteObject method on the context instance. This will also remove the association between the comment and the article (and also between the comment and the project if I'm right).

You can also put a brea kpoint after the call to SaveChanges and see the query that gets executed against the SQL Server with IntelliTrace (or use SQL profiles as a last resort).

Regards...

Padel
Ah, this actually worked as I wanted.Only "problem" now is that I have to get a list of all the associated comments, and enumerating through them calling DeleteObject. Works just fine, of course, but I would really like to see a bit more elegant way of removing many to many relationship entities...
Yngve B. Nilsen
Unfortunately I don't know another way of doing this with EF (and neither by using EntitySQL).
Padel
Alright, thanks! :)
Yngve B. Nilsen