title: Hierarchical Databinding with Linq to SQL
body: <p>I am attempting what I think is a rather simple databinding scenario with Linq To SQL.</p>
  <p>I have a table of FacultyMembers, whose schema looks something like this:</p>
  <ul>
  <li>FacultyMemberID - int PK</li>
  <li>Name - nvarchar</li>
  <li>UniversityID - int FK to University table</li>
  </ul>
  <p>and so forth. There are various other string properties.</p>
  <p>I generate LTS DataClasses. I drop a LinqDataSource and a GridView on a page, enable update and delete for both, and I'm on my merry way. No code, and I'm able to update my string properties. A little manipulation with a DropDownList on the UniversityID and I'm able to update that one-to-many relationship, too. Yay.</p>
  <p>Now, lets say I throw in a many-to-many mapping table. Let's say DivisionMemberships, which maps FacultyMembers to Divisions. DivisionMembership uses the simple and obvious schema:</p>
  <ul>
  <li>FacultyMemberID - int PK, FK to FacultyMembers table</li>
  <li>DivisionID - int PK, FK to Divisions table</li>
  </ul>
  <p>Now, when I take a row of my GridView into EditMode, I run into a problem because I don't know how to update the many-to-many relationship. I mucked around with a few alternatives, and right now I'm trying to get a ListView to work in there. I'm doing something like this:</p>
  <pre><code>&lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;
      AllowPaging=&quot;True&quot; AllowSorting=&quot;True&quot; PageSize=&quot;25&quot; DataKeyNames=&quot;FacultyMemberID&quot; &gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField=&quot;Name&quot; HeaderText=&quot;Name&quot; SortExpression=&quot;Name&quot; /&gt;
          &lt;asp:TemplateField HeaderText=&quot;University&quot; SortExpression=&quot;UniversityID&quot;&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;University.Name&quot;) %&gt;' /&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:DropDownList ID=&quot;DropDownList2&quot; runat=&quot;server&quot; 
                      DataSourceID=&quot;LinqDataSourceUniversities&quot; DataTextField=&quot;Name&quot; 
                      DataValueField=&quot;UniversityID&quot; SelectedValue='&lt;%# Bind(&quot;UniversityID&quot;) %&gt;'&gt;
                  &lt;/asp:DropDownList&gt;
                  &lt;asp:LinqDataSource ID=&quot;LinqDataSourceUniversities&quot; runat=&quot;server&quot; 
                      ContextTypeName=&quot;NYDERHE.NYDERHEDataClassesDataContext&quot; 
                      Select=&quot;new (UniversityID, Name)&quot; TableName=&quot;Universities&quot;&gt;
                  &lt;/asp:LinqDataSource&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText=&quot;Division&quot;&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:ListView ID=&quot;ListView1&quot; runat=&quot;server&quot;
                      InsertItemPosition=&quot;LastItem&quot; DataSource='&lt;%# Eval(&quot;DivisionMemberships&quot;) %&gt;'&gt;&lt;ItemTemplate&gt;
                          &lt;li style=&quot;&quot;&gt;FacultyMemberID:
                              &lt;asp:Label ID=&quot;FacultyMemberIDLabel&quot; runat=&quot;server&quot; 
                                  Text='&lt;%# Eval(&quot;FacultyMemberID&quot;) %&gt;' /&gt;
                              &lt;br /&gt;
                              DivisionID:
                              &lt;asp:Label ID=&quot;DivisionIDLabel&quot; runat=&quot;server&quot; 
                                  Text='&lt;%# Eval(&quot;DivisionID&quot;) %&gt;' /&gt;
                              &lt;br /&gt;
                              Division:
                              &lt;asp:Label ID=&quot;DivisionLabel&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;Division&quot;) %&gt;' /&gt;
                              &lt;br /&gt;
                              FacultyMember:
                              &lt;asp:Label ID=&quot;FacultyMemberLabel&quot; runat=&quot;server&quot; 
                                  Text='&lt;%# Eval(&quot;FacultyMember&quot;) %&gt;' /&gt;
                              &lt;br /&gt;
                              &lt;asp:Button ID=&quot;DeleteButton&quot; runat=&quot;server&quot; CommandName=&quot;Delete&quot; 
                                  Text=&quot;Delete&quot; /&gt;
                          &lt;/li&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;/asp:ListView&gt;
                  &lt;/EditItemTemplate&gt;
  </code></pre>
  <p>and so forth. Some of the chatter above is removed, but ListView is pretty verbose as-is, so I don't want to overload the page.</p>
  <p>Things to note here:</p>
  <ul>
  <li>For my University association, I use a new LinqDataSource and query for items WHERE the UniversityID matches, then bind the new UniversityID (DDL value) to the FacultyMember, whereas for the DivisionMemberships, I bind directly to the property (as described <a href="http://msdn.microsoft.com/en-us/library/aa478959.aspx#aspn-hierdatabinding_topic7" rel="nofollow noreferrer">here</a>)</li>
  <li>I use DataBinder.<strong>Bind()</strong> for UniversityID, whereas I use DataBinder.<strong>Eval()</strong> for DivisionMemberships.</li>
  </ul>
  <p>If I switch to Bind() for DivisionMemberships, I get a NotSerializableException for EntitySet. If I stick with Eval(), I have to write the OnDelete and OnInsert methods for the ListView myself, and I don't want to Delete or Insert the DivisionMemberships until the entire FacultyMember row exits EditMode. I'd probably create a DataContext and stick it in the session for this, because I don't have another way to mark the DivisionMemberships for updates.</p>
  <p>I would think that this scenario would be pretty easy to enable out of the box, but I'm lost. Any advice on where to go from here? Specifically, should I wrestle with Bind() and try to make EntitySet serializable, should I bite the bullet and write the somewhat hackish code described above for storing a DataContext in session until the OnRowUpdating event fires, or am I going the wrong way completely?</p>