Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you break circular associations between entities?
    primarykey
    data
    text
    <p>my first time on the site so apologies if it's tagged incorrectly or been answered elsewhere...</p> <p>I keep running into particular situation on my current project and I was wondering how you guys would deal with it. The pattern is: a parent with a collection of children, and the parent has one or more references to particular items in the child collection, normally the 'default' child.</p> <p>A more concrete example:</p> <pre><code>public class SystemMenu { public IList&lt;MenuItem&gt; Items { get; private set; } public MenuItem DefaultItem { get; set; } } public class MenuItem { public SystemMenu Parent { get; set; } public string Name { get; set; } } </code></pre> <p>To me this seems like a good clean way of modelling the relationship, but causes problems immediately thanks to the circular association, I can't enforce the relationship in the DB because of the circular foreign keys, and LINQ to SQL blows up due to the cyclic association. Even if I could bodge my way round this, it's clearly not a great idea.</p> <p>My only idea currently is to have an 'IsDefault' flag on MenuItem:</p> <pre><code>public class SystemMenu { public IList&lt;MenuItem&gt; Items { get; private set; } public MenuItem DefaultItem { get { return Items.Single(x =&gt; x.IsDefault); } set { DefaultItem.IsDefault = false; value.DefaultItem = true; } } } public class MenuItem { public SystemMenu Parent { get; set; } public string Name { get; set; } public bool IsDefault { get; set; } } </code></pre> <p>Has anyone dealt with something similar and could offer some advice?</p> <p>Cheers!</p> <p>Edit: Thanks for the responses so far, perhaps the 'Menu' example wasn't brilliant though, I was trying to think of something representative so I didn't have to go into the specifics of our not-so-self-explanatory domain model! Perhaps a better example would be a Company/Employee relationship:</p> <pre><code>public class Company { public string Name { get; set; } public IList&lt;Employee&gt; Employees { get; private set; } public Employee ContactPerson { get; set; } } public class Employee { public Company EmployedBy { get; set; } public string FullName { get; set; } } </code></pre> <p>The Employee would definitely need a reference to their Company, and each Company could only have one ContactPerson. Hope this makes my original point a bit clearer!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload