title: Dynamic Inheritance Mapping in LINQ
body: <p>This is an inheritance mapping that works when the Types: Customer &amp; Employee are known to me.</p>
  
  <pre class="lang-cs prettyprint-override"><code>modelBuilder.Entity&lt;Person&gt;()
              .Map&lt;Customer&gt;(c =&gt; c.Requires("Type").HasValue("Customer"))
              .Map&lt;Employee&gt;(e =&gt; e.Requires("Type").HasValue("Employee"))
              .ToTable("People", "dbo");
  </code></pre>
  
  <p>In my case, the <b>Customer</b> and <b>Employee</b> classes will reside in plugin assemblies. So, I am trying to create the mapping using reflection. </p>
  
  <p><b>NOTE:</b> To keep the example simple I have hard coded the types. Also, I am only trying to create the mapping for the <b>Customer</b> type.</p>
  
  <pre class="lang-cs prettyprint-override"><code>const BindingFlags binding = BindingFlags.Public | BindingFlags.Instance;
  
  EntityTypeConfiguration&lt;Person&gt; entityConfig = modelBuilder.Entity&lt;Person&gt;();
  var entityConfigType = entityConfig.GetType();
  
  // Map Method
  MethodInfo mapMethod = null;
  var methods = entityConfigType.GetMethods(binding);
  foreach (var method in methods)
  {
      if (method.Name == "Map" &amp;&amp; method.IsGenericMethod)
      {
          mapMethod = method.MakeGenericMethod(typeof(Customer));
          break;
      }
  }
  
  // Requires Method
  Type[] requiresArgType = new Type[]
      {
          typeof(string)
      };
  
  var mappingConfigType = typeof(EntityMappingConfiguration&lt;Customer&gt;);
  var requiresMethod = mappingConfigType.GetMethod("Requires", binding, null, requiresArgType, null);
  
  // Has Value Method
  var vccType = typeof (ValueConditionConfiguration);
  Type[] hasValueArgType = new Type[]
      {
          typeof(string)
      };
  
  var hasValueMethod = vccType.GetMethod("HasValue", binding, null, hasValueArgType, null);
  
  var param1 = Expression.Parameter(typeof (EntityMappingConfiguration&lt;Customer&gt;), "c");
  var requiresCall1 = Expression.Call(param1, requiresMethod, Expression.Constant("Type"));
  var hasValueCall1 = Expression.Call(requiresCall1, hasValueMethod, Expression.Constant("Customer"));
  
  var lambda1 = Expression.Lambda(hasValueCall1, param1);
  var @delegate1 = lambda1.Compile();
  
  mapMethod.Invoke(entityConfig, new object[]
      {
          @delegate1
      });
  </code></pre>
  
  <p>When trying to invoke the <b>Map</b> method, the following Exception is thrown:-</p>
  
  <pre><code>Object of type 'System.Func`2[System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1[ConsoleApplication1.Customer],System.Data.Entity.ModelConfiguration.Configuration.StringColumnConfiguration]' cannot be converted to type 'System.Action`1[System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1[ConsoleApplication1.Customer]]'.
  </code></pre>
  
  <p>How do I convert </p>
  
  <pre><code>Func&lt;EntityMappingConfiguration&lt;Customer&gt;&gt; with return type of StringColumnConfiguration
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Action&lt;EntityMappingConfiguration&lt;Customer&gt;&gt;
  </code></pre>