EF2: Is there a way to add an Entity to EntityCollection without having to load related Entity?

Posted by mutex on Stack Overflow See other posts from Stack Overflow or by mutex
Published on 2010-05-25T02:24:58Z Indexed on 2010/05/25 2:31 UTC
Read the original article Hit count: 276

Filed under:
|

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);

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework