Efficient way to deal with maintaining Many:Many relationships in EF Code-First I've got this all working, but it seems to be quite long-winded, and I thought I'd post here and see if I'm doing it wrong...

I have a M:M relationship between an Installer and a MasterInstance. The classes (code-first) look like:

public class MasterInstance
{
    .. rest of fields here ..
    public virtual ICollection&lt;Installer&gt; PermittedInstallers { get; set; }
}

public class Installer
{
    .. rest of fields here ..
    public virtual ICollection&lt;MasterInstance&gt; PermittedMasterInstances { get; set; }
}


I want to be able to edit these in my Installer view, using a multi-list box. So, I create a ViewModel for what I need:

public class InstallerViewModel
{
    public Installer Installer { get; set; }
    public List&lt;MasterInstance&gt; PermittedMasterInstances { get; set; }
    public int[] SelectedMasterInstances { get; set; }
}


And then I place this in my view like so so that I can select the instances for my installer:

&lt;div class="editor-field"&gt;
    @Html.ListBoxFor(model =&gt; model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option =&gt; new SelectListItem {
        Text = option.Name, 
        Value = option.MasterInstanceId.ToString()
}))
&lt;/div&gt;


In the controller, for the edit action, I need to set this view model up:

var installer = context.Installers.Include(i =&gt; i.PermittedMasterInstances).Single(x =&gt; x.InstallerId == installerId);
            InstallerViewModel model = new InstallerViewModel
                                           {
                                               Installer = installer,
                                               PermittedMasterInstances = context.MasterInstances.ToList(),
                                               SelectedMasterInstances = installer.PermittedMasterInstances.Select(i =&gt; i.MasterInstanceId).ToArray()
                                           };
return View(model);


Finally, on the post of the edit, I need to delete any relationships that are no longer there and add the new ones:

// Grab the model from the viewmodel and attach to the context
var installer = installerModel.Installer;
context.Installers.Attach(installer);

// Load the related records (dont know why Lazy Loading wouldn't kick in here)
context.Entry(installer).Collection(i =&gt; i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi =&gt; !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi =&gt; installer.PermittedMasterInstances.Remove(mi));

// Now loop through an int[] and add those new relations, WITHOUT the pain of fetching them from the DB
foreach (var permittedMasterInstanceId in installerModel.SelectedMasterInstances)
{
    if (!installer.PermittedMasterInstances.Any(pmi =&gt; pmi.MasterInstanceId == permittedMasterInstanceId))
    {
        var masterInstance = new MasterInstance {MasterInstanceId = permittedMasterInstanceId};
        context.MasterInstances.Attach(masterInstance);
        installer.PermittedMasterInstances.Add(masterInstance);
    }
}

// We're done - save and finish.
context.Entry&lt;Installer&gt;(installer).State = EntityState.Modified;
context.SaveChanges();


So this works... But, it seemed like a lot of effort, is this the right/best way to achieve it?
I know this is an old question, but I think the problem is timeless. Many to many associations (i.e. without junction class) in Entity Framework are always independent associations, so you can only establish or remove them by manipulating object collections, not primitive key values. Inefficiency is inherent to the implementation.  

But it is not prohibited to have a second context that only contains junction tables.

You could create a context that contains the MasterInstanceInstaller junction table and use this to update the associations in the most efficient way you can get using EF:

var installer = installerModel.Installer;

var junctions = context.MasterInstanceInstallers
                       .Where(x =&gt; x.InstallerId == installer.InstallerId)
                       .ToList();

// Delete deselected instances.
foreach(var mi in junctions
                  .Where(x =&gt; !installerModel.SelectedMasterInstances
                                             .Contains(x.MasterInstanceId)))
{
    context.MasterInstanceInstallers.Remove(mi);
}

// Add newly selected instances.
foreach(int instanceId in installerModel.SelectedMasterInstances
                                        .Except(junctions.Select(j =&gt; j.MasterInstanceId)))
}
    context.MasterInstanceInstallers.Add(new MasterInstanceInstaller
                                             {
                                                InstallerId = installer.InstallerId,
                                                MasterInstanceId = instanceId
                                             }
                                        );
}
context.SaveChanges();


Now, if necessary you can populate the updated many to many association through the main context.