views:

65

answers:

1

I can set an EntityReference on an entity without having to load the relevent entity as follows:

this.CategoryReference.EntityKey = new EntityKey("MyEntities.CategorySet", "Id", 12);

So I can set the Category for the entity I'm dealing with to whatever category has id 12 without having to hit the database.

But is there a way to do this or something similar on an EntityCollection? So if I now have multiple categories instead of just one, I want to now do something like the following, though it doesn't work:

// stand-in category
var categoryStandIn = new Category { EntityKey = new EntityKey("MyEntities.CategorySet", "Id", 12) }
this.Categories.Add(categoryStandIn);
+1  A: 

I asked a simular question. Use stub entities.

http://stackoverflow.com/questions/2410741/entitycollections-setting-an-entityreference-entitykey-to-populate-the-collection

John Hartsock
Thank you John! I had tried that previously, but the comments in the blog you linked contained the clues I needed to fix it - turns out the entity I was trying to add had already been loaded so I had to call ObjectStateManager.TryGetObjectStateEntry before doing the AttachTo to check if it was already loaded - if already loaded use the loaded entity, if not use the stub.
mutex