title: Get Type from EF Core Context
body: <p>I am trying to get the <code>type</code> of an entity from a given string. My end goal is to make an interface (UI interface) for users to have a GUI to explore my Database. They basically have a tree of tables and their fields and as they select fields, I want to build up the SQL query on the backend and return the data. I'm getting stuck on how to get the Type.</p>
  
  <p>I'm trying to staple together a solution between these threads:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/26305737/how-do-i-select-correct-dbset-in-dbcontext-based-on-table-name">How do I select correct DbSet in DbContext based on table name</a></li>
  <li><a href="https://stackoverflow.com/questions/47910340/how-to-get-dbset-from-entity-name-in-ef-core-net-core-2-0/51958834#51958834">How to get DbSet from entity name in EF Core / .NET Core 2.0</a></li>
  </ul>
  
  <p>Here's my current attempt(s):</p>
  
  <pre><code>var type1 = _dbContext.Model.FindEntityType("Chadwick.Database.Entities.Appearances");
  var type = _dbContext.Appearances.GetType();
  var context = _dbContext.Set(typeof(Appearance)); // this works. I just need to pass in a variable instead of the actual type
  var stuff = await context.FromSql("SELECT TOP 100 * FROM Appearances").ToListAsync();
  // var data = await _dbContext.Appearances.Select(a =&gt; new {a.PlayerId}).Take(100).ToListAsync();
  return new OkObjectResult(stuff);
  </code></pre>
  
  <p>Essentially, I will never know that they are after "Appearances", so I can't just provide the exact type, I need to fetch it (by string) from the context.</p>
  
  <p><strong>Is there a way to get the actual type by string?</strong></p>
  
  <p>I know I could do something like this, but it seems redundent (there's about 20 tables and will be more)</p>
  
  <pre><code>public Type GetTypeByName(string name)
  {
      switch (name)
      {
          case "Appearances":
              return typeof(Appearance);
          case "AwardsManagers":
              return typeof(AwardsManager);
      }
  
      return null;
  }
  </code></pre>