title: Generic Add Method in Entity Framework -- Trouble Passing-in Type from String Name
body: <p>I admit that a large part of my trouble with this must stem from simple lack of understanding around a few of the components involved in the solution. I'm not exactly "new to" Entity Framework, code-first, nor generic types, but the inner workings of all three are still mysterious enough to me that this is giving me fits.</p>
  
  <p>I have a <strong>code-first</strong> project in which I have separated-out "model" classes from "service" classes, with generalizations in both. I am <strong>NOT</strong> using the full <strong>Repository Pattern</strong>, for a variety of reasons. For most of what I am doing, the structure I have in place is working beautifully -- I understand it and it seems quite clean.</p>
  
  <p>But there is one area where I am running into problems, and that is being able to pass one of my model class types as a generic parameter to an instance of a generic service object, given a string path/name of the model class. </p>
  
  <p><em>(Background: I need to do this because I "seed" several tables in the database with initialization values from a JSON file. This JSON file contains the names of the model entities. So, at runtime, I need to get that string value, and then feed that as the type to the generic service object that does the database operations.)</em></p>
  
  <p>Here are the pertinent code snippets:</p>
  
  <p>In <strong>BaseEntity.cs</strong> I have the top-level interface and a number of abstract classes from which the specific model entities then inherit:</p>
  
  <pre><code>namespace POST.API.Models
  {
      public interface IEntity
      {
          int Id { get; set; }
      }
  
      public abstract class BaseEntity { }
  
      public abstract class Entity : BaseEntity, IEntity
      {
          public virtual int Id { get; set; }
      }
  
      public abstract class TypeEntity : Entity
      {
          public TypeDefinition Definition { get; set; }
      }
  }
  </code></pre>
  
  <p>In <strong>BaseService.cs</strong> I have another interface and more abstract classes from which specific model service classes inherit. There is also one concrete class, here, that is generalized for performing an insert operation:</p>
  
  <pre><code>namespace POST.API.Services
  {
      public interface IEntityService { }
  
      public abstract class BaseEntityService&lt;T&gt; : IEntityService
          where T : Models.BaseEntity
      {
          public T Fetch(int Id)
          {
              using (var Db = new PostDbContext())
              {
                  return Db.Set&lt;T&gt;().Find(Id);
              }
          }
  
          public void Create(T Item)
          {
              if (Item != null)
              {
                  using (var Db = new PostDbContext())
                  {
                      DbSet Entity = Db.Set&lt;T&gt;();
  
                      Entity.Add(Item);
                      Db.SaveChanges();
                  }
              }
          }
  
          public IEnumerable&lt;T&gt; All()
          {
              using (var Db = new PostDbContext())
              {
                  return (IEnumerable&lt;T&gt;)Db.Set&lt;T&gt;().ToList();
              }
          }
      }
  
      public abstract class BaseTypeEntityService&lt;T&gt; : BaseEntityService&lt;T&gt;
          where T : Models.TypeEntity
      { }
  
      public abstract class BasePropertyTypeEntityService&lt;T&gt; : BaseTypeEntityService&lt;T&gt;
          where T : Models.PropertyTypeEntity { }
  
      public abstract class BasePropertyEntityService&lt;T&gt; : BaseEntityService&lt;T&gt;
          where T : Models.BaseEntity { }
  
      public class TypeEntityService&lt;T&gt; : BaseTypeEntityService&lt;T&gt;
          where T : Models.TypeEntity { }
  
      #endregion
  }
  </code></pre>
  
  <p>I've removed some methods not pertinent to the presentation.</p>
  
  <p>I have some code that then attempts to use these base classes to rummage through the JSON file and insert some rows, thus:</p>
  
  <pre><code>        using (PostDbContext Db = new PostDbContext())
          {
              string JsonString = System.IO.File.ReadAllText(JsonDataFile);
              DataSet JsonDataSet = JsonConvert.DeserializeObject&lt;DataSet&gt;(JsonString);
              foreach (DataTable Table in JsonDataSet.Tables)
              {
                  Type EType = Type.GetType("POST.API.Models." + Table.TableName);
                  POST.API.Models.BaseEntity E = (POST.API.Models.BaseEntity)Activator.CreateInstance(EType);
                  Services.TypeEntityService&lt;EType&gt; S = new Services.TypeEntityService&lt;EType&gt;();
  
                  foreach (DataRow Row in Table.Rows)
                  {
                      // Set properties of E and call Create method of S
                  }
              }
          }
  </code></pre>
  
  <p>I've clearly misunderstood something fundamental, because that code won't compile. On this line of code:</p>
  
  <pre><code>Services.TypeEntityService&lt;EType&gt; S = new Services.TypeEntityService&lt;EType&gt;();
  </code></pre>
  
  <p>...I get an error on my references to EType, with the compiler complaining <strong>"The type or namespace `EType' could not be found."</strong></p>
  
  <p>So, obviously, that reference, there, cannot be evaluated at runtime. Which, then, makes me wonder how on earth do I do this. All the related topics seem to yield no satisfactory answer -- at least not in a way that makes sense in the context of my own implementation.</p>