locked
Many to Many Insert RRS feed

  • Question

  • Hello all,

    I'm trying to perform a many to many insert with the entity framework. After looking around for a while I decided to post the question.

    I have three tables:

    Member
    Platform
    FavouritePlatform (which contains the MemberId and PlatformId)

    I am trying to insert a member and add their favourite platforms.

                for (int i = 0; i < favouritePlatformCheckList.Items.Count; i++)
                {
                    if (favouritePlatformCheckList.Items[i].Selected)
                    {
                        Platform platform = new Platform();
                        platform.EntityKey = 
                            new System.Data.EntityKey("CVGSEntities.Platforms",
                            "Code", favouritePlatformCheckList.Items[i].Value);
                        member.FavouritePlatforms.Add(platform);
    // Adds the member to the entity context and calls save changes on the context.
      memberManager.Add(member);
    } }
    The code above does not and I believe it is because I am specifying the platform entity key and when its trying to add it it already exists.

    Is there anyway to add the favourite platforms without having to do a query for them first? (I have the Ids of the platforms I want to add).

    Thanks for any help.
    Wednesday, November 25, 2009 11:52 PM

Answers