title: LINQ to SQL Insert (one to many relationship using custom entity classes and [Association] attribute)
body: <p>I am currently delving into LINQ to SQL as it looks cool, and potentially much more useful than ADO.NET in many circumstances.</p>
  
  <p>I have used LINQ to SQL before, but I created all the domain classes using the LINQ to SQL Class designer, and I don't really want to use it as I would rather know what is going on 'behind the scenes' rather than putting it down .NET magic. Hence, I have been attempting to create my domain (or entity) classes manually using LINQ attributes.</p>
  
  <p>I was finding it all ok untill I ran into problems whilst trying to model a one to many relationship in my DB. There is a table <strong>Meets</strong> that has a column <strong>LeaderId</strong>, which is a foreign key to the table <strong>Members</strong></p>
  
  <p>This is how I have the entity classes set up:</p>
  
  <p><strong>Meet.cs</strong></p>
  
  <pre><code>[Table(Name = "dbo.Meets")]
  public class Meet
  {
      private EntityRef&lt;Member&gt; _leader;
  
      [Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int MeetId { get; set; }
  
      [Column]
      public string Location { get; set; }
  
      [Column]
      public DateTime Date { get; set; }
  
      [Association(Name = "FK_Meets_Members", Storage = "_leader")]
      public Member Leader
      {
          get { return _leader.Entity; }
          set { _leader.Entity = value; }
      }
  }
  </code></pre>
  
  <p><strong>Member.cs</strong></p>
  
  <pre><code>[Table(Name = "dbo.Members")]
      public class Member
      {
          private EntitySet&lt;Meet&gt; _meets;
  
          [Column(IsPrimaryKey = true, IsDbGenerated = true)]
          public int MemberId { get; set; }
  
          [Column]
          public string Name { get; set; }
  
          [Column]
          public string Hometown { get; set; }
  
          [Column]
          public string Info { get; set; }
  
          [Column]
          public ComitteeRole ComitteeRole { get; set; }
  
          [Association(Name = "FK_Meets_Members", Storage = "_meets")]
          public ICollection&lt;Meet&gt; Meets
          {
              get { return _meets; }
              set { _meets.Assign(value); }
          }
      }
  </code></pre>
  
  <p>Selects work on the Meets table and likewise on the Members table and associated entities are loaded fine. However when I try and do an insert into the Meets table I am getting the good old <em>'Object reference not set to an instance of an object'</em> error.</p>
  
  <p>Now the problem must lie with what I am using to store the the <strong>LeaderId</strong> when I am adding a new Meet entity. I am setting the foreign key in my object like so:</p>
  
  <pre><code>Meet meet = new Meet();
  
  meet.Leader.MemberId = 1;
  </code></pre>
  
  <p>However, when I am doing the LINQ insert it doesn't seem to understand that the MemberId of the Leader property is the value to put in the foreign key column in the <strong>Meets</strong> table.</p>
  
  <p>I realise this could probably be solved by adding a separate property to the <strong>Meet</strong> class like so:</p>
  
  <pre><code>[Column]
  public int LeaderId { get; set; }
  </code></pre>
  
  <p>However, I don't want to clutter my class with an extra property that is a duplicate of the MemberId property of the Leader property.</p>
  
  <p>Hope you understand me...
  Can anyone help?</p>