title: Is it possible to create a generic method for adding items to a entity framework dbset?
body: <p>I have not worked with Entity Framework or generics before and am having some difficulty reducing my code.</p>
  
  <p>I am parsing a text file to load 10 lookup tables with data that could possibly change nightly. The text files have a heading for the "type" followed by a list of key/value sets. I have this working perfectly, but I would like to refactor the code to clean it up and would like to use generic methods to accomplish this so I can reduce the duplicated code.</p>
  
  <p>I have gotten the parsing down to a generic method, but I have not been able to figure out how to get the entities added to the context in a generic way. I have to loop over a list of the entities for each type and add them to the context:</p>
  
  <pre><code>void Main()
  {
      switch (line.ToUpper())
      {
          case "AREA":
          {
              List&lt;Area&gt; areaList = this.GetItems&lt;Area&gt;(file);
  
              foreach (var element in areaList)
              {
                  if (context.Area.Find(element.Id) == null)
                  {
                      context.Area.Add(element);
                  }
              }
  
              break;
          }
          case "CATEGORY":
          {
              List&lt;Category&gt; categoryList = this.GetItems&lt;Category&gt;(file);
  
              foreach (var element in categoryList)
              {
                  if (context.Category.Find(element.Id) == null)
                  {
                      context.Category.Add(element);
                  }
              }
  
              break;
          }
      }
  }
  
  private List&lt;T&gt; GetItems&lt;T&gt;(StreamReader reader) where T : ILookupData, new()
  {
      string line;
      List&lt;T&gt; list = new List&lt;T&gt;();            
  
      while (reader.Peek() == ' ')
      {
          line = reader.ReadLine().TrimStart();
          string[] tokens = line.Split(new string[] { " - " }, 2, StringSplitOptions.None);
  
          T item = new T();
          item.Id = Convert.ToInt32(tokens[0]);
          item.Description = (string)tokens[1];
  
          list.Add(item);
      }
  
      return list;
  }
  </code></pre>
  
  <p><strong>Update</strong>: The above works fine, but cannot seem to get the context added.</p>
  
  <p>I have tried a few different things, but seem to keep getting this error when I try to generic up the context:</p>
  
  <blockquote>
    <p>The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type of method.</p>
  </blockquote>
  
  <p>The last thing I tried was to add a generic GetDbSet to the context:</p>
  
  <pre><code>public DbSet&lt;T&gt; GetDbSet&lt;T&gt;() where T : class
  {
      return this.Set&lt;T&gt;();
  }
  </code></pre>
  
  <p>But I get the same error in my controller adding this code to the GetItems method:</p>
  
  <pre><code>using (MyContext context = new MyContext())
  {
      var dbSet = context.GetDbSet&lt;T&gt;();
  }
  </code></pre>