A look at the top 2 answers to the question "How to delete many-to-many relationship in Entity Framework without loading all of the data" (see the article) suggests that an Attach method exists for navigation-property collections (i.e., entity1.Entity2Collection.Attach(entity2)). The problem is, I have VS 2015 / EF 6.1.3 (VB and DBContext) and that method doesn't seem to exist for navigation-property collections (or at least Intellisense doesn't show it). Am I doing something wrong?
Asked
Active
Viewed 39 times
0

Community
- 1
- 1

Robert Gustafson
- 31
- 4
-
You are right, there is no such method available for navigation property collections. You may look at this post http://stackoverflow.com/questions/39708439/ef-create-remove-relation-from-many-to-many-relations-when-autodetectchangesen, not sure if it applies to your case. – Ivan Stoev Jan 09 '17 at 13:21
-
Attach is not part of Entity 6.1.3, it has been removed in some previous versions – Antoine Pelletier Jan 09 '17 at 14:03
-
I figured out the first answer on the page with the question I linked to wasn't the right one for EF 6.1.3; the second answer (the one by dannie.f) is the right one. You have to create a detached instance of the first entity, Add the second entity to its nav-prop collection, Attach the (unnattached) first entity, then do the Remove. – Robert Gustafson Jan 09 '17 at 18:46
-
If one wanted to ADD a relationship between 2 entitites without loading them first, I suppose that dannie.f's code could still be used--just replace the Remove statement with Add, right? – Robert Gustafson Jan 26 '17 at 07:53
-
I think I know things work. EF only write CHANGES to an entity's properties to the store. Therefore, it's not necessary for the "absolute" state of an entity's properties--including collection navigation properties--to be correct when SaveChanges or DetectChanges is called so long as the alterations--additions, deletions, modifications--made since the last Attach/SaveChanges/DetectChanges/query was made are correct. BTW, what's the best way to create a DETACHED clone of an attached entity? – Robert Gustafson Feb 03 '17 at 22:55
-
To ADD (instead of delete) without loading data, dannie.f's code should have the Remove on the unattached entity omitted and the Remove made after the Attach-ment changed to add. Whether adding or removing, you want the realted item to be absent or present, respectively, in the collection before you Attach and then Add or Remove. Am I right? – Robert Gustafson Feb 03 '17 at 22:59
1 Answers
0
Dannie f.'s solution (re-coded for VB) is as follows, assuming a model of TopicDBEntities, and entity-collections of Topic and Subscription:
Dim db = New TopicDBEntities() ' Create UNATTACHED instances of the two entities and associate them ' (In real life, you would have something more sophisticated for the ' next 2 lines) Dim topic = New Topic { .TopicId = 1 } Dim subscription = New Subscription { .SubscriptionId = 2} topic.Subscriptions.Add(subscription) ' Attach the topic and subscription as unchanged ' so that they will not be added to the db ' but start tracking changes to the entities db.Topics.Attach(topic) ' Remove the subscription ' EF will know that the subscription should be removed from the topic topic.subscriptions.Remove(subscription) ' commit the changes db.SaveChanges()

Robert Gustafson
- 31
- 4