title: C# .Net Core Utilize generic property in base controller class utilizing DbSet<TEntity>.FromSql
body: <p>We have a group of controllers that return JSON data for to sync entities via a Web API. Each data entity has its own controller and data model ( "Order, Item, Customer, ..." )
  So, this essentially has every controller using the exact same code.  The only difference is name of the controller/web api path, and the data model that will be returned via JSON.
  What we would like to do is have one base controller (BaseController.cs) that all other controllers implement/extend. To do this, we want to have a virtual "EntityType" or "EntityName" property in the base class that each child controller class defines.  Then, our code to return the data does not have to be repeated within every controller.
  Our problem is we can't figure out how to store/pass/define a "type" or "TEntity" variable.  And we need this to execute the main part of our "Get" api method:</p>
  
  <p>Example:</p>
  
  <pre><code>var returnValue = dbContext.Set&lt;GetEntityModelClass()&gt;().FromSql("EXEC dbo.[AJ_P_{0}_API]", GetEntityModelName()).ToList();
  </code></pre>
  
  <p>We implement GetEntityModelName() in the BaseController.cs class like this:</p>
  
  <pre><code>public virtual string GetEntityName() { return null; }
  </code></pre>
  
  <p>and an example of a child controller class would have the following:</p>
  
  <pre><code>public override string GetEntityName() { return "Product"; }
  </code></pre>
  
  <p>How can we do this for a comparable GetEntityModelClass() that could pass the value that dbContext.Set() call needs?</p>
  
  <p>In case it helps, here is a larger sample of our code:</p>
  
  <pre><code>BaseController.cs
  -----------------------------------------
  namespace API.Controllers
  {
      [Produces("application/json")]
      public class BaseController : Controller
      {
  
          // Returns the name of an entity as it is used in stored procedures, class names, etc...
          public virtual string GetEntityName()
          {
              return null;
          }
          // Return a type that can be utilized with the dbContext.Set&lt;???&gt;() call
          public virtual ??? GetEntityName()
          {
              return ???;
          }
          // SiteContext is the API site's data context.
          protected readonly SiteContext dbContext;
          public BaseController(SiteContext context)
          {
              dbContext = context;
          }
  
          [HttpGet("{token}")]
          public IActionResult Get([FromRoute] string token)
          {
              return Get(token);
          }
          public IActionResult Results(string token)
          { 
              if (!ModelState.IsValid) { return BadRequest(ModelState); }
  
              var returnValue = dbContext.Set&lt;???GetEntityModel()???&gt;().FromSql("EXEC dbo.[AJ_P_{0}_API] @Token={1}", GetEntityName(), token).ToList();
  
              return Ok(returnValue);
          }
      }
  }
  
  ProductController.cs
  -----------------------------------------
  
  namespace API.Controllers
  {
      [Produces("application/json")]
      [Route("/Product")]
      public class ProductController : BaseController
      {
          public override string GetEntityName() {
              return "Product";
          }
          public override ???? GetEntityModel() {
              return ( ??? ProductModel type ??? )
          }
  
          public ProductController(SiteContext context) : base(context) { }
  
      }
  }
  </code></pre>