title: Many-to-many mapping with LINQ
body: <p>I would like to perform LINQ to SQL mapping in C#, in a many-to-many relationship, but where data is not mandatory.<br>
  To be clear:<br>
  I have a news site/blog, and there's a table called <code>Posts</code>. A blog can relate to many categories at once, so there is a table called <code>CategoriesPosts</code> that links with foreign keys with the <code>Posts</code> table and with <code>Categories</code> table. I've made each table with an identity primary key, an <code>id</code> field in each one, if it matters in this case.<br>
  In C# I defined a class for each table, defined each field as explicitly as possible. The <code>Post</code> class, as well as <code>Category</code> class, have a <code>EntitySet</code> to link to <code>CategoryPost</code> objects, and <code>CategoryPost</code> class has 2 <code>EntityRef</code> members to link to 2 objects of each other type.</p>
  
  <p>The problem is that a Post may relate or not to any category, as well as a category may have posts in it or not. I didn't find a way to make an <code>EntitySet&lt;CategoryPost?&gt;</code> or something like that.</p>
  
  <p>So when I added the first post, all went well with not a single SQL statement. Also, this post was present in the output. When I tried to add the second post I got an exception, <b>Object reference not set to an instance of an object</b>, regarding to the <code>CategoryPost</code> member.</p>
  
  <p>Post:</p>
  
  <pre><code>[Table(Name="tm_posts")]
  public class Post : IDataErrorInfo
  {
      public Post()
      {
          //Initialization of NOT NULL fields with their default values
      }
  
      [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
      public int ID { get; set; }
  
      private EntitySet&lt;CategoryPost&gt; _categoryRef = new EntitySet&lt;CategoryPost&gt;();
      [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_categoryRef", ThisKey = "ID", OtherKey = "PostID")]
      public EntitySet&lt;CategoryPost&gt; CategoryRef
      {
          get { return _categoryRef; }
          set { _categoryRef.Assign(value); }
      }
  }
  </code></pre>
  
  <p>CategoryPost</p>
  
  <pre><code>[Table(Name = "tm_rel_categories_posts")]
  public class CategoryPost
  {
      [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
      public int ID { get; set; }
  
      [Column(Name = "fk_post", DbType = "int", CanBeNull = false)]
      public int PostID { get; set; }
  
      [Column(Name = "fk_category", DbType = "int", CanBeNull = false)]
      public int CategoryID { get; set; }
  
      private EntityRef&lt;Post&gt; _post = new EntityRef&lt;Post&gt;();
      [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_post", ThisKey = "PostID", OtherKey = "ID")]
      public Post Post
      {
          get { return _post.Entity; }
          set { _post.Entity = value; }
      }
  
      private EntityRef&lt;Category&gt; _category = new EntityRef&lt;Category&gt;();
      [Association(Name = "tm_rel_categories_posts_fk", IsForeignKey = true, Storage = "_category", ThisKey = "CategoryID", OtherKey = "ID")]
      public Category Category
      {
          get { return _category.Entity; }
          set { _category.Entity = value; }
      }
  }
  </code></pre>
  
  <p>Category</p>
  
  <pre><code>[Table(Name="tm_categories")]
  public class Category
  {
      [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
      public int ID { get; set; }
  
      [Column(Name = "fk_parent", DbType = "int", CanBeNull = true)]
      public int ParentID { get; set; }
  
      private EntityRef&lt;Category&gt; _parent = new EntityRef&lt;Category&gt;();
      [Association(Name = "tm_posts_fk2", IsForeignKey = true, Storage = "_parent", ThisKey = "ParentID", OtherKey = "ID")]
      public Category Parent
      {
          get { return _parent.Entity; }
          set { _parent.Entity = value; }
      }
  
      [Column(Name = "name", DbType = "varchar(100)", CanBeNull = false)]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>So what am I doing wrong? How to make it possible to insert a post that doesn't belong to any category? How to insert categories with no posts?</p>