Reputation: 2866
I am using EF on a utility I am working on and have run into an issue that is rather frustrating. I have three tables: Template, Target, and TemplateTarget but when I add these three tables to the EDMX I wind up with only two because TemplateTarget is a lookup table that contains the relationships between Template and Target. Obviously EF is smart enough to figure this out but it poses an issue if I want to break (delete) a relationship.
When I issue a delete, in my given scenario, I need to delete from the lookup table and NOT from the physical tables themselves. The 'right' answer would be to rework the datamodel but that isn't something that can be done at this juncture. Is there anyway to force EF to separate this lookup table out so that I can issue deletes directly against it or am I going to be limited to issuing a DELETE statement directly to the underlying table?
Upvotes: 1
Views: 201
Reputation: 364249
If you just want to delete from lookup table it means you want to delete a relation which is done this way:
template.Targets.Remove(target);
Your template must be attached to the context and target must be the related entity. It will just remove the relation from the database but not those entities.
Upvotes: 2