title: In LINQtoSQL, how can I update child relationships by editing the parent model?
body: <p>This is my first attempt at updating a database using LINQtoSQL. At least, it's my first if you don't count the tutorials I've followed. Unfortunately, The tutorials that I have found don't offer much more than updating a single table. I'm attempting to update a DB Model that's a bit more complex.</p>
  
  <p>I have a <code>Stream</code> table:</p>
  
  <pre><code>[Table]
  public class Stream
  {
      [HiddenInput(DisplayValue = false)]
      [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
      public long StreamID { get; set; }
  
      /** Other columns removed for brevity **/
  
      // relationship:
      private EntitySet&lt;Stream2FieldTypes&gt; _Stream2FieldTypes;
      [System.Data.Linq.Mapping.Association(Storage = "_Stream2FieldTypes", OtherKey = "StreamID")]
      public EntitySet&lt;Stream2FieldTypes&gt; Stream2FieldTypes
      {
          get { return this._Stream2FieldTypes; }
          set { this._Stream2FieldTypes.Assign(value); }
      }
  </code></pre>
  
  <p>And I have a <code>Stream2FieldTypes</code> table:</p>
  
  <pre><code>[Table]
  public class Stream2FieldTypes
  {
      [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
      public long s2fID { get; set; }
      public long StreamID { get; set; }     // FK
  
      /** other columns removed for brevity **/
  
      // relationship (one Stream2FieldTypes to many Streams) 
      private EntitySet&lt;Stream&gt; _Stream;
      [Association(Storage = "_Stream", ThisKey = "StreamID")]
      public EntitySet&lt;Stream&gt; Stream
      {
          get { return this._Stream; }
          set { this._Stream.Assign(value); }
      }
  </code></pre>
  
  <p>Now, I am trying to update the model so I can send updates to the repository to persist to <code>DataContext</code>. I can't update <code>Stream.Stream2FieldTypes</code> because the <code>get</code> is set to a private <code>EntitySet</code>.</p>
  
  <p>How do I update <code>Stream.Stream2FieldTypes</code> when I can't change <code>Stream.Stream2FieldTypes</code> because it's a private EntitySet&lt;>?</p>
  
  <h2>Edit: psuedo code</h2>
  
  <p>Basically, I think I should be able to update the <code>Stream</code> and <code>Stream2FieldTypes</code> tables by using a command in my Edit Action like this:</p>
  
  <pre><code>myRepository.SaveStream(stream);
  </code></pre>
  
  <p>I have been trying to do something like this:</p>
  
  <pre><code>        if (ModelState.IsValid)
          {
              // Convert StreamEditModel to Stream
              var stream = new Genesis.Domain.Entities.Stream
              {
                  StreamID = form.StreamID,
                  StreamUrl = form.StreamUrl,
                  StreamName = form.StreamName,
                  StreamBody = form.StreamBody,
                  StreamTitle = form.StreamTitle,
                  StreamKeywords = form.StreamKeywords,
                  StreamDescription = form.StreamDescription,
                  Stream2FieldTypes = new EntitySet&lt;Stream2FieldTypes&gt;()
              };
  
              // Loop to convert Stream2FieldTypes to Steam2FieldTypesEditModel
              foreach (var item in form.Stream2FieldTypes)
              {
                  var fieldTypeEntry = new Stream2FieldTypes
                  {
                      FieldTypeID = item.FieldTypeID,
                      s2fID = item.s2fID,
                      s2fIsRequired = item.s2fIsRequired,
                      s2fLabel = item.s2fLabel,
                      StreamID = item.StreamID,
                  };
                  stream.Stream2FieldTypes.Add(fieldTypeEntry); // Add to list
              }
  
              genesisRepository.SaveStream(stream);
  
              return RedirectToAction("Index");
          }
          else 
          {
              return View(form);
          }
  </code></pre>
  
  <p>When I try to run this code, I get this error:</p>
  
  <blockquote>
    <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
    
    <p>Source Error: </p>
    
    <p>Line 58:         {</p>
    
    <p>Line 59:             get { return this._Stream2FieldTypes; }</p>
    
    <p>line with error: Line 60:             set { this._Stream2FieldTypes.Assign(value); }</p>
    
    <p>Line 61:         }</p>
    
    <p>Line 62: </p>
  </blockquote>