title: Best way to save type of class in database?
body: <p>What is a good way to denote <strong>"type"</strong> in database?</p>
  
  <p>I have a base class <code>Action</code> which is inherited by numerous child classes. <code>Action</code> has members like <code>Id</code>, <code>Name</code> etc all which corresponds to equivalent columns in a table in the database called <code>action</code>. So <code>action</code> table looks like this:</p>
  
  <pre><code>id | name | type 
  </code></pre>
  
  <p>The <code>type</code> column denotes what action it is. In my code they correspond to the child classes deriving from parent <code>Action</code>. Now how do I save the <strong>type of class</strong> to the <strong>type field in database</strong>?</p>
  
  <p>The type column in db could be of any data type.</p>
  
  <p>Options:</p>
  
  <ol>
  <li><p>Save <code>action.GetType().ToString()</code> as string in db. And get the action type from db back by converting the string representation of the type to its original type using reflection. <strong>But this will be problematic if class names change in future.</strong></p></li>
  <li><p>Create an enum to denote each child class and decorate it by a <code>TypeAttribute</code>, something like:</p>
  
  <pre><code>public abstract class Action
  {
      public enum Kind 
      {
          [Type(typeof(ControlAction))]
          ControlAction = 1, 
  
          [Type(typeof(UpdateAction))]
          UpdateAction = 2, 
  
          etc 
      }
  
      public abstract Kind ActionType { get; }
  }
  
  public class ControlAction : Action { public override Kind ActionType { get { return Kind.ControlAction; } } }
  public class UpdateAction : Action { public override Kind ActionType { get { return Kind.UpdateAction; } } }
  //etc
  </code></pre>
  
  <p><strong>This looks good, except that for each class from here onwards I have to create an enum.</strong> And it feels like a little too much work to be done.</p></li>
  <li><p>Build a separate static hash table of <code>&lt;int, Type&gt;</code> that ties a class to a <code>int</code> value. <strong>May be a little bit unreadable.</strong></p></li>
  </ol>
  
  <p>Is there a better solution to this?</p>