Efficient way to deal with maintaining Many:Many relationships in EF Code-FirstImplementing a generic DropDownList attribute and templating solutionEnabling discard pending changes on DbContextCaliburn Micro communication between list and edit viewmodelsSwing MVC and Command PatternIs it an anti-pattern to let ViewModel fill itself from a domain object?Setting user permissions, where certain permissions imply other permissionsCMS controllers, from procedural to object orientedPHP MVC RESTful APIHow to optimize code to only pull separate table rows that are necessaryWinform, with a workaround to avoid dependencies

Not using 's' for he/she/it

Longest common substring in linear time

It grows, but water kills it

Is this toilet slogan correct usage of the English language?

Drawing ramified coverings with tikz

Creepy dinosaur pc game identification

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Strong empirical falsification of quantum mechanics based on vacuum energy density

Pre-mixing cryogenic fuels and using only one fuel tank

Why did the EU agree to delay the Brexit deadline?

Why can Carol Danvers change her suit colours in the first place?

Aragorn's "guise" in the Orthanc Stone

What if a revenant (monster) gains fire resistance?

Delivering sarcasm

Is there a name for this algorithm to calculate the concentration of a mixture of two solutions containing the same solute?

Multiplicative persistence

Is it better practice to read straight from sheet music rather than memorize it?

Added a new user on Ubuntu, set password not working?

What was this official D&D 3.5e Lovecraft-flavored rulebook?

How to bake one texture for one mesh with multiple textures blender 2.8

Redundant comparison & "if" before assignment

Store Credit Card Information in Password Manager?

C++ debug/print custom type with GDB : the case of nlohmann json library



Efficient way to deal with maintaining Many:Many relationships in EF Code-First


Implementing a generic DropDownList attribute and templating solutionEnabling discard pending changes on DbContextCaliburn Micro communication between list and edit viewmodelsSwing MVC and Command PatternIs it an anti-pattern to let ViewModel fill itself from a domain object?Setting user permissions, where certain permissions imply other permissionsCMS controllers, from procedural to object orientedPHP MVC RESTful APIHow to optimize code to only pull separate table rows that are necessaryWinform, with a workaround to avoid dependencies













10












$begingroup$


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<Installer> PermittedInstallers get; set;


public class Installer

.. rest of fields here ..
public virtual ICollection<MasterInstance> 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<MasterInstance> 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:



<div class="editor-field">
@Html.ListBoxFor(model => model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option => new SelectListItem
Text = option.Name,
Value = option.MasterInstanceId.ToString()
))
</div>


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



var installer = context.Installers.Include(i => i.PermittedMasterInstances).Single(x => x.InstallerId == installerId);
InstallerViewModel model = new InstallerViewModel

Installer = installer,
PermittedMasterInstances = context.MasterInstances.ToList(),
SelectedMasterInstances = installer.PermittedMasterInstances.Select(i => 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 => i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi => !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi => 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 => 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<Installer>(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?










share|improve this question











$endgroup$











  • $begingroup$
    This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
    $endgroup$
    – Luke McGregor
    Feb 15 '12 at 21:42
















10












$begingroup$


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<Installer> PermittedInstallers get; set;


public class Installer

.. rest of fields here ..
public virtual ICollection<MasterInstance> 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<MasterInstance> 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:



<div class="editor-field">
@Html.ListBoxFor(model => model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option => new SelectListItem
Text = option.Name,
Value = option.MasterInstanceId.ToString()
))
</div>


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



var installer = context.Installers.Include(i => i.PermittedMasterInstances).Single(x => x.InstallerId == installerId);
InstallerViewModel model = new InstallerViewModel

Installer = installer,
PermittedMasterInstances = context.MasterInstances.ToList(),
SelectedMasterInstances = installer.PermittedMasterInstances.Select(i => 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 => i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi => !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi => 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 => 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<Installer>(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?










share|improve this question











$endgroup$











  • $begingroup$
    This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
    $endgroup$
    – Luke McGregor
    Feb 15 '12 at 21:42














10












10








10


2



$begingroup$


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<Installer> PermittedInstallers get; set;


public class Installer

.. rest of fields here ..
public virtual ICollection<MasterInstance> 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<MasterInstance> 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:



<div class="editor-field">
@Html.ListBoxFor(model => model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option => new SelectListItem
Text = option.Name,
Value = option.MasterInstanceId.ToString()
))
</div>


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



var installer = context.Installers.Include(i => i.PermittedMasterInstances).Single(x => x.InstallerId == installerId);
InstallerViewModel model = new InstallerViewModel

Installer = installer,
PermittedMasterInstances = context.MasterInstances.ToList(),
SelectedMasterInstances = installer.PermittedMasterInstances.Select(i => 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 => i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi => !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi => 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 => 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<Installer>(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?










share|improve this question











$endgroup$




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<Installer> PermittedInstallers get; set;


public class Installer

.. rest of fields here ..
public virtual ICollection<MasterInstance> 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<MasterInstance> 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:



<div class="editor-field">
@Html.ListBoxFor(model => model.SelectedMasterInstances,(Model.PermittedMasterInstances).Select(option => new SelectListItem
Text = option.Name,
Value = option.MasterInstanceId.ToString()
))
</div>


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



var installer = context.Installers.Include(i => i.PermittedMasterInstances).Single(x => x.InstallerId == installerId);
InstallerViewModel model = new InstallerViewModel

Installer = installer,
PermittedMasterInstances = context.MasterInstances.ToList(),
SelectedMasterInstances = installer.PermittedMasterInstances.Select(i => 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 => i.PermittedMasterInstances).Load();
// Iterate and delete existing relationships
var instancesToDelete = installer.PermittedMasterInstances.Where(mi => !installerModel.SelectedMasterInstances.Contains(i.MasterInstanceId)).ToList();
instancesToDelete.ForEach(mi => 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 => 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<Installer>(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?







c# mvc entity-framework asp.net-mvc-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '14 at 21:14









Jamal

30.4k11121227




30.4k11121227










asked Feb 1 '12 at 8:47









Matt RobertsMatt Roberts

1513




1513











  • $begingroup$
    This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
    $endgroup$
    – Luke McGregor
    Feb 15 '12 at 21:42

















  • $begingroup$
    This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
    $endgroup$
    – Luke McGregor
    Feb 15 '12 at 21:42
















$begingroup$
This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
$endgroup$
– Luke McGregor
Feb 15 '12 at 21:42





$begingroup$
This is exactly how ive approached this problem in the past, its a bit ugly but ive never fund a better way
$endgroup$
– Luke McGregor
Feb 15 '12 at 21:42











2 Answers
2






active

oldest

votes


















5












$begingroup$

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 => x.InstallerId == installer.InstallerId)
.ToList();

// Delete deselected instances.
foreach(var mi in junctions
.Where(x => !installerModel.SelectedMasterInstances
.Contains(x.MasterInstanceId)))

context.MasterInstanceInstallers.Remove(mi);


// Add newly selected instances.
foreach(int instanceId in installerModel.SelectedMasterInstances
.Except(junctions.Select(j => 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.






share|improve this answer









$endgroup$












  • $begingroup$
    +1 It's never too late to shoot a zombie!
    $endgroup$
    – Mathieu Guindon
    Mar 27 '14 at 0:03


















0












$begingroup$

It definitely is timeless. I've done it with and without the explicit junction table over the years but for the first time I find myself wanting the best of both worlds needing both:



  1. "mutual collections" approach
    that is in effect by omitting the junction table in the model

  2. Explicit junction table in model approach
    Letting one grapple with joins

I'm currently considering using a view to avoid explicit inclusion of junction table in the model in order to let me do the joins I might sometimes need and to also shape the returned data with additional selection criteria in the ON clauses used to join each side of the many-to-many relationship. Will report back as to the success of failure of this approach.





share








New contributor




Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$












    Your Answer





    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f8537%2fefficient-way-to-deal-with-maintaining-manymany-relationships-in-ef-code-first%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5












    $begingroup$

    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 => x.InstallerId == installer.InstallerId)
    .ToList();

    // Delete deselected instances.
    foreach(var mi in junctions
    .Where(x => !installerModel.SelectedMasterInstances
    .Contains(x.MasterInstanceId)))

    context.MasterInstanceInstallers.Remove(mi);


    // Add newly selected instances.
    foreach(int instanceId in installerModel.SelectedMasterInstances
    .Except(junctions.Select(j => 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.






    share|improve this answer









    $endgroup$












    • $begingroup$
      +1 It's never too late to shoot a zombie!
      $endgroup$
      – Mathieu Guindon
      Mar 27 '14 at 0:03















    5












    $begingroup$

    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 => x.InstallerId == installer.InstallerId)
    .ToList();

    // Delete deselected instances.
    foreach(var mi in junctions
    .Where(x => !installerModel.SelectedMasterInstances
    .Contains(x.MasterInstanceId)))

    context.MasterInstanceInstallers.Remove(mi);


    // Add newly selected instances.
    foreach(int instanceId in installerModel.SelectedMasterInstances
    .Except(junctions.Select(j => 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.






    share|improve this answer









    $endgroup$












    • $begingroup$
      +1 It's never too late to shoot a zombie!
      $endgroup$
      – Mathieu Guindon
      Mar 27 '14 at 0:03













    5












    5








    5





    $begingroup$

    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 => x.InstallerId == installer.InstallerId)
    .ToList();

    // Delete deselected instances.
    foreach(var mi in junctions
    .Where(x => !installerModel.SelectedMasterInstances
    .Contains(x.MasterInstanceId)))

    context.MasterInstanceInstallers.Remove(mi);


    // Add newly selected instances.
    foreach(int instanceId in installerModel.SelectedMasterInstances
    .Except(junctions.Select(j => 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.






    share|improve this answer









    $endgroup$



    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 => x.InstallerId == installer.InstallerId)
    .ToList();

    // Delete deselected instances.
    foreach(var mi in junctions
    .Where(x => !installerModel.SelectedMasterInstances
    .Contains(x.MasterInstanceId)))

    context.MasterInstanceInstallers.Remove(mi);


    // Add newly selected instances.
    foreach(int instanceId in installerModel.SelectedMasterInstances
    .Except(junctions.Select(j => 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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 26 '14 at 21:17









    Gert ArnoldGert Arnold

    1,51111322




    1,51111322











    • $begingroup$
      +1 It's never too late to shoot a zombie!
      $endgroup$
      – Mathieu Guindon
      Mar 27 '14 at 0:03
















    • $begingroup$
      +1 It's never too late to shoot a zombie!
      $endgroup$
      – Mathieu Guindon
      Mar 27 '14 at 0:03















    $begingroup$
    +1 It's never too late to shoot a zombie!
    $endgroup$
    – Mathieu Guindon
    Mar 27 '14 at 0:03




    $begingroup$
    +1 It's never too late to shoot a zombie!
    $endgroup$
    – Mathieu Guindon
    Mar 27 '14 at 0:03













    0












    $begingroup$

    It definitely is timeless. I've done it with and without the explicit junction table over the years but for the first time I find myself wanting the best of both worlds needing both:



    1. "mutual collections" approach
      that is in effect by omitting the junction table in the model

    2. Explicit junction table in model approach
      Letting one grapple with joins

    I'm currently considering using a view to avoid explicit inclusion of junction table in the model in order to let me do the joins I might sometimes need and to also shape the returned data with additional selection criteria in the ON clauses used to join each side of the many-to-many relationship. Will report back as to the success of failure of this approach.





    share








    New contributor




    Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$

















      0












      $begingroup$

      It definitely is timeless. I've done it with and without the explicit junction table over the years but for the first time I find myself wanting the best of both worlds needing both:



      1. "mutual collections" approach
        that is in effect by omitting the junction table in the model

      2. Explicit junction table in model approach
        Letting one grapple with joins

      I'm currently considering using a view to avoid explicit inclusion of junction table in the model in order to let me do the joins I might sometimes need and to also shape the returned data with additional selection criteria in the ON clauses used to join each side of the many-to-many relationship. Will report back as to the success of failure of this approach.





      share








      New contributor




      Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$















        0












        0








        0





        $begingroup$

        It definitely is timeless. I've done it with and without the explicit junction table over the years but for the first time I find myself wanting the best of both worlds needing both:



        1. "mutual collections" approach
          that is in effect by omitting the junction table in the model

        2. Explicit junction table in model approach
          Letting one grapple with joins

        I'm currently considering using a view to avoid explicit inclusion of junction table in the model in order to let me do the joins I might sometimes need and to also shape the returned data with additional selection criteria in the ON clauses used to join each side of the many-to-many relationship. Will report back as to the success of failure of this approach.





        share








        New contributor




        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        $endgroup$



        It definitely is timeless. I've done it with and without the explicit junction table over the years but for the first time I find myself wanting the best of both worlds needing both:



        1. "mutual collections" approach
          that is in effect by omitting the junction table in the model

        2. Explicit junction table in model approach
          Letting one grapple with joins

        I'm currently considering using a view to avoid explicit inclusion of junction table in the model in order to let me do the joins I might sometimes need and to also shape the returned data with additional selection criteria in the ON clauses used to join each side of the many-to-many relationship. Will report back as to the success of failure of this approach.






        share








        New contributor




        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share






        New contributor




        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 7 mins ago









        Pat PattilloPat Pattillo

        11




        11




        New contributor




        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Pat Pattillo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f8537%2fefficient-way-to-deal-with-maintaining-manymany-relationships-in-ef-code-first%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

            Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar