title: Complex class hierarchies in C#
body: <p>I have to create the class model which will contain 4 classes - Database, Schema, Table and Column, something like:</p>
  
  <pre><code>public class Database
  {
      public string Name { get; set; }
  
      public List&lt;Schema&gt; Schemas { get; set; };
      public List&lt;Table&gt; Tables { get; set; };
      public List&lt;Column&gt; Columns { get; set; };
  }
  
  public class Schema
  {
      public string Name { get; set; }
  
      public Database Database { get; set; }
      public List&lt;Table&gt; Tables { get; set; };
  }
  
  public class Table
  {
      public string Name { get; set; }
  
      public Schema Schema { get; set; }
      public List&lt;Column&gt; Columns { get; set; };
  }
  
  public class Column
  {
      public string Name { get; set; }
  
      public Schema Table { get; set; }
  }
  </code></pre>
  
  <p>Well, the class hierarchy is pretty easy: Database->Schemas->Tables->Columns</p>
  
  <p>As you can see there are the "Tables" and "Columns" - lists in the Database - class which should contain all tables and all columns in the database.</p>
  
  <p>What is the better way to synchronize the Database.Tables - list and the Schema.Tables - list? And the Database.Columns and Table.Columns lists?
  It should be possible to add a new table to the Database.Tables and to see the new table in the according Schema.Tables - list. Or to add a new table to the Schema.Tables and see new table in the Database.Tables list. </p>
  
  <p>I can define the Schema.Tables property as:</p>
  
  <pre><code>    public List&lt;Table&gt; Tables
      {
          get { return Database.Tables.Where(p =&gt; p.Schema == this).ToList&lt;Tables&gt;(); }
      }
  </code></pre>
  
  <p>But in this if I add new table to the Schema.Tables it will not appear in the Database.Tables list.</p>
  
  <p>What is the better way to solve this problem?</p>
  
  <p>PS. Sorry for my terrible English!</p>