title: MongoDB and derived class
body: <p>I am having trouble saving a particular class with MongoDB.</p>
  
  <p>My abstract class is as follow:</p>
  
  <pre><code>public abstract class Enumeration : IComparable
  {
  public string Name { get; }
  public int Id { get; }
  
  protected Enumeration(int id, string name)
  {
    Id = id;
    Name = name;
  }
  
  public override string ToString() =&gt; Name;
  
  public static IEnumerable&lt;T&gt; GetAll&lt;T&gt;() where T : Enumeration
  {
    var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
  
    return fields.Select(f =&gt; f.GetValue(null)).Cast&lt;T&gt;();
  }
  
  public override bool Equals(object obj)
  {
    var otherValue = obj as Enumeration;
  
    if (otherValue == null)
      return false;
  
    var typeMatches = GetType().Equals(obj.GetType());
    var valueMatches = Id.Equals(otherValue.Id);
  
    return typeMatches &amp;&amp; valueMatches;
  }
  
  public override int GetHashCode() =&gt; Id.GetHashCode();
  
  public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
  {
    var absoluteDifference = Math.Abs(firstValue.Id - secondValue.Id);
    return absoluteDifference;
  }
  
  public static T FromValue&lt;T&gt;(int value) where T : Enumeration
  {
    var matchingItem = Parse&lt;T, int&gt;(value, "value", item =&gt; item.Id == value);
    return matchingItem;
  }
  
  public static T FromDisplayName&lt;T&gt;(string displayName) where T : Enumeration
  {
    var matchingItem = Parse&lt;T, string&gt;(displayName, "display name", item =&gt; item.Name == displayName);
    return matchingItem;
  }
  
  private static T Parse&lt;T, K&gt;(K value, string description, Func&lt;T, bool&gt; predicate) where T : Enumeration
  {
    var matchingItem = GetAll&lt;T&gt;().FirstOrDefault(predicate);
  
    if (matchingItem == null)
      throw new InvalidOperationException($"'{value}' is not a valid {description} in {typeof(T)}");
  
    return matchingItem;
  }
  
  public int CompareTo(object other) =&gt; Id.CompareTo(((Enumeration)other).Id);
  }
  </code></pre>
  
  <p>The derived class:</p>
  
  <pre><code>public class SeatState : Enumeration
  {
  public static SeatState Free = new SeatState(1, nameof(Free).ToLowerInvariant());
  public static SeatState Booked = new SeatState(2, nameof(Booked).ToLowerInvariant());
  public static SeatState Reserved = new SeatState(3, nameof(Reserved).ToLowerInvariant());
  
  public static IEnumerable&lt;SeatState&gt; List() =&gt; new[] { Free, Booked, Reserved };
  
  public SeatState(int id, string name)
    : base(id, name)
  {
  }
  
  public static SeatState FromName(string name)
  {
    var state = List().SingleOrDefault(s =&gt; string.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));
  
    if (state == null)
      throw new Exception($"Possible values for SeatState: {string.Join(",", List().Select(s =&gt; s.Name))}");
  
    return state;
  }
  
  public static SeatState From(int id)
  {
    var state = List().SingleOrDefault(s =&gt; s.Id == id);
  
    if (state == null)
      throw new Exception($"Possible values for SeatState: {string.Join(",", List().Select(s =&gt; s.Name))}");
  
    return state;
  }
  }
  </code></pre>
  
  <p>When I try to save a class that have <code>SeatState</code> as a property, mongo throw this error:</p>
  
  <p><em>System.ArgumentOutOfRangeException: The memberInfo argument must be for class SeatState, but was for class Enumeration.</em></p>
  
  <p>I tried also with <code>BsonClassMap.RegisterClassMap&lt;Enumeration&gt;()</code> (with and without <code>IsRootClass</code>) and <code>BsonClassMap.RegisterClassMap&lt;SeatState&gt;()</code>
  but without success. It seems that the problem lie in the static properties, but I need them.</p>
  
  <p>Any clarification in what I am doing wrong is greatly appreciated.</p>