title: Entity Framework Code First GenericTypeArguments
body: <p>I have the unfortunate task to create an MVC project that uses both an MySQL and MSSQL database. To do that I use Entity Framework 6 code first and its working on both databases.</p>
  
  <p>The problem however is that MSSQL supports Schemas and MYSQL does not. If I add the <code>modelBuilder.Entity&lt;Status&gt;().ToTable("Status", schemaName: "Client");</code> I get an error building the MySQL database.</p>
  
  <p>To solve this I tried adding a custom attribute to all my DbSets to be able to determine the schema I want to use. In MSSQL I will use the schema and in MYSQL I will prefix the table with the schema name.</p>
  
  <p>EG:</p>
  
  <pre><code>MSSQL:
  Client.Status
  Employee.Status
  
  MYSQL:
  Client_Status
  Employee_Status
  </code></pre>
  
  <p>Now to determine the types and schemas I want to use reflection. Unfortunately I cannot use the <code>GenericTypeArguments</code> in the <code>OnModelCreating</code> method. It says <code>'System.Type' does not contain a definition for 'GenericTypeArguments' and ...</code></p>
  
  <p>However, if I copy the reflection code to an action in my MVC Controller (seperate project), it does work.</p>
  
  <pre><code>    [TableSchema("Status", "Client")]
      public DbSet&lt;Tables.Status&gt; Status { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          //launch debugger to see inspector
          if (System.Diagnostics.Debugger.IsAttached == false)
              System.Diagnostics.Debugger.Launch();
  
          //use schema or not
          var useSchema = ConfigurationManager.AppSettings["UseSchema"] == "true";
  
          //get all properties
          PropertyInfo[] properties = typeof(DataContext).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  
          foreach (PropertyInfo p in properties)
          {
              // Only work with dbsets
              if (p.PropertyType.Name != "DbSet`1") { continue; }
  
              //get tableschema attribute
              var attribute = p.GetCustomAttributes(typeof(TableSchema), false).First();
  
  
              //the line below reports the error on building
              var type = p.PropertyType.GenericTypeArguments.First().Name;
  
  
              //the goal is to use something like 
              modelBuilder.Entity&lt;type&gt;().ToTable(attribute.Table, attribute.Schema);
  
          }
      }
  </code></pre>
  
  <p>If I uncomment the line with <code>var type =</code> and enable debugging, I am also able to see the <code>GenericTypeArguments</code> in the inspector.</p>
  
  <p>So how can I use the GenericTypeArguments or something similar to dynamically get the <code>Tables.Status</code> type or an alternative to alter schemas and tablenames dynamically.</p>
  
  <p>UPDATE:</p>
  
  <p>I managed to get the type by casting it to a dynamic first. But then the second part of the code as introduced by Moho fails</p>
  
  <pre><code>            var type = ((dynamic)p).PropertyType.GenericTypeArguments[0];
  
              if (type != null)
              {
                  var t = modelBuilder.GetType();
                  var m = t.GetMethod("Entity", BindingFlags.Public);
  
                  //System.NullReferenceException
                  var mgm = m.MakeGenericMethod((Type)type);
  
  
                  var entityTypeConfig = mgm.Invoke(modelBuilder, new object[] { });
  
                  if (!useSchema)
                  {
                      entityTypeConfig.GetType()
                          .GetMethods()
                          .Single(mi =&gt; mi.Name == "ToTable" &amp;&amp; mi.GetParameters().Count()==1)
                          .Invoke(entityTypeConfig, new object[] { attribute.Schema + "_" + attribute.Table });
                  }
                  else
                  {
                      entityTypeConfig.GetType()
                          .GetMethods()
                          .Single(mi =&gt; mi.Name == "ToTable" &amp;&amp; mi.GetParameters().Count() == 2)
                          .Invoke(entityTypeConfig, new object[] { attribute.Table, attribute.Schema });
                  }
  
  
              }
  </code></pre>