0

I was wondering if there's any way to achieve something like this using EF4:

I would like to do something like :

1. |   'Get Parent
2. |   Dim Parent as oParent = mContext.GetParent(parentId) 
3. | 
4. |   'Link a child without loading it entirely, only using his ID
5. |   oParent.Children.AddOnlyUsingChildId(aChildId) 'Or maybe EntityKey?
6. |
8. |   'Apply Changes (Save)
7. |   mContext.SaveChanges()

All this because, I don't want to make a request to server each time to get whole child entity and anyway, I only have child's ID (comes from a checkboxlist). This is part a loop, in a loop and may have to load a hundreds of children for each first level loop. In my point of view, it is possible to do that as Entity Graph should know that when it comes the time to save the parent and N..N relation, he should only insert 'Parrent.nId and Child.nId' in DB mapping table between child and parent... so it should not need entire child's graph.

Am I right? Is it possible?

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
  • You may find some help with entity splitting: http://stackoverflow.com/questions/10236723/how-do-i-prevent-entity-framework-from-loading-a-filestream-column-into-a-byte-a – Garrison Neely Aug 05 '13 at 15:44

1 Answers1

2

You can do this by creating a "stub entity" and attaching it to the context. Example with C# syntax:

var child = new Child { ID = aChildId };
mContext.Children.Attach(child);
oParent.Children.Add(child);
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Well, quite simple finaly. Thanks @Slauma. I'd try something similar ; `mContext.Attach(child);` but get an error when trying it... But in your way, this is working. Thanks again. – Simon Dugré Aug 05 '13 at 16:42
  • By any chance, a side question, do you know if there's a way to know if a child collection has changed (New child or something)? – Simon Dugré Aug 05 '13 at 18:59
  • @SimonDugré: I think only be querying the database and comparing the original collection with the collection from your view. – Slauma Aug 05 '13 at 19:23