Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can have any relationship where a particular item can have multiple items associated with it using IEnumerable (or some other collection) properties. In your example, this would look as follows:</p> <pre><code>class Participant { public IEnumerable&lt;Championship&gt; Championships { get { return _championships; } } private List&lt;Championship&gt; _championships; } </code></pre> <p>Some important things to keep in mind:</p> <ul> <li><p>Always make this a read-only property. This is sometimes confusing to people, especially if you have something modifiable like an ICollection returned rather than an IEnumerable. Making the property read-only doesn't prevent modification of the <em>collection</em> but modification of the <em>entire list</em>.</p></li> <li><p>Loading strategies - You'll notice in the example above the collection isn't initalized. You typically do this either in the constructor or when the property is first accessed (called lazy instantiation) - generally speaking lazy instantiation adds some complexity but can increase performance, especially if this collection is not used often or you have a lot of these sorts of properties.</p></li> <li><p>Generally, it's a good idea to pick a class to "hold" the other class in the case of many-to-many (i.e. participants have championships properties but championships do not have participants properties or vice versa). This cuts down on the amount of code you have to write, and reduces complexity and your surface area with your database. If a call is needed for the other direction, consider a method call rather than a property. Remember, many-to-many from a relational sense doesn't necessarily mean that's the common use case (As a user, I might only want to add participants to championships rather than championships to participants)</p></li> <li><p>If your collection is modifiable, remember that sending a Save implies that collections underneath it should be modified as well if they were changed. This can add a good deal of complexity (In the past, I've used internal lists to store added / deleted items)</p></li> </ul>
 

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