After drinking cold coffee in agony for 24 hours over the Insert functionality in Linq to Entities, I finally stumbled up on a good solution. I had been getting errors like .. “The member with identity ‘Entities’ does not exist in the metadata collection. Parameter name: identity” in the Context.AddObject() method.
First of all I found the core place to be for my concerns and questions. The ADO.Net forum and then I found this post which really got me on the track.
Ended up solving all my problems with the following when inserting an object with 2 foreign keys; Course and User which are of types (Course and User); for which I don’t want to retrieve the objects from the database just to add them as the foreign keys for the insert. The eventual solution for the insert looks like this (with the root problem bolded).
public void InsertRound(Round round)
round.UserReference.EntityKey = new EntityKey(DBContext.DefaultContainerName + “.User”, “Id”, round.IdUser);
round.CourseReference.EntityKey = new EntityKey(DBContext.DefaultContainerName + “.Course”, “Id”, round.IdCourse);