title: Casting a generic entity using reflection with generic method
body: <p>I have a method called <code>RevertChanges</code> which looks like the following:</p>
  
  <pre><code>public static void RevertChanges&lt;TEntity&gt;()
  {
              var jsonData = DataAccess.GetEnvironmentJson((int)Environments.Production);
              var model = JsonConvert.DeserializeObject&lt;Models.FullDataSetModel&gt;(jsonData);
              var targetEntity = typeof(TEntity).Name;
              using (var db = new LoginPageContentEntities())
              {
                  db.Database.ExecuteSqlCommand(String.Format("TRUNCATE TABLE [{0}]", targetEntity));
                  var modelValue = model.GetType().GetProperty(targetEntity).GetValue(model, null);
                  var table = (DbSet)(typeof(LoginPageContentEntities).GetProperty(targetEntity).GetValue(db, null));
                  table.AddRange((DbSet)modelValue);                         
                  db.SaveChanges();
              }
  }
  </code></pre>
  
  <p>I know that whatever <code>TEntity</code> is will match the modelValue data set. DbSet casting does not work and I am struggling to find how you can return an entire entity with generics.  I have found that if I cast with <code>(IEnumerable&lt;TEntity&gt;)</code> I am able to call <code>ToList()</code> on table, however this will not work as I need to be able to call add range.  Is it possible to:</p>
  
  <ol>
  <li>Return an entity reference with generics? </li>
  <li>Access <code>AddRange</code> and then
  save the context?</li>
  </ol>
  
  <p>For context I am trying to replace this method:</p>
  
  <pre><code>public static void RevertChanges(Environments targetToRestoreFrom, ContentFields tableToRevert)
          {
              var jsonData = DataAccess.GetEnvironmentJson((int)targetToRestoreFrom);
              var model = JsonConvert.DeserializeObject&lt;Models.FullDataSetModel&gt;(jsonData);            
              using (var db = new LoginPageContentEntities())
              {
                  switch (tableToRevert)
                  {
                      case ContentFields.ProductInformation:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [ProductInformation]");
                          db.ProductInformation.AddRange(model.ProductInformation);
                          break;
                      case ContentFields.DidYouKnow:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [DidYouKnow]");
                          db.DidYouKnow.AddRange(model.DidYouKnow);
                          break;
                      case ContentFields.MaintenanceMessage:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [MaintenanceMessage]");
                          db.MaintenanceMessage.AddRange(model.MaintenanceMessage);
                          break;
                      case ContentFields.TrainingEvent:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [TrainingEvents]");
                          db.TrainingEvents.AddRange(model.TrainingEvents);
                          break;
                      case ContentFields.VideoContent:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [HtmlSnippets]");
                          db.HtmlSnippets.AddRange(model.HtmlSnippets);
                          break;
                      case ContentFields.ProductMarketingUrl:
                          db.Database.ExecuteSqlCommand("TRUNCATE TABLE [ProductMarketingUrl]");
                          db.ProductMarketingUrl.AddRange(model.ProductMarketingUrl);
                          break;
                  }                
                  db.SaveChanges();
              }
          }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>namespace LoginPageContentManager.Models
  {
      public class FullDataSetModel
      {
          public List&lt;DidYouKnow&gt; DidYouKnow                   { get; set; }
          public List&lt;HtmlSnippets&gt; HtmlSnippets               { get; set; }
          public List&lt;MaintenanceMessage&gt; MaintenanceMessage   { get; set; }
          public List&lt;ProductInformation&gt; ProductInformation   { get; set; }
          public List&lt;TrainingEvents&gt; TrainingEvents           { get; set; }
          public List&lt;ProductMarketingUrl&gt; ProductMarketingUrl { get; set; }
      }
  }
  </code></pre>