title: Passing a dbset as a parameter to a function
body: <p>I couldn't quite find the answer I was looking for searching through stack so Im posting this. </p>
  
  <p>Given this context</p>
  
  <pre><code>  public ProjectionsContext()
          : base("name=ProjectionsDatabase")
      {
      }
  
      public virtual DbSet&lt;Projection&gt; Projections { get; set; }
  
      public virtual DbSet&lt;Symbol&gt; Symbols { get; set; }
  
      public virtual DbSet&lt;TradePriceData&gt; TradePriceDatas { get; set; }
  
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
  
          **//set all decimal properties in Projection Entity to be Required
          var decimalproperties = typeof(Projection).GetProperties()
                                    .Where(p =&gt; p.PropertyType == typeof(decimal));
          foreach (var property in decimalproperties)
          {
              var lambda = CreateLambdaExpression&lt;Projection, decimal&gt;(property);
              modelBuilder.Entity&lt;Projection&gt;()
                  .Property(lambda)
                  .IsRequired();
          }**
  
          //add the custom configurations for each entity
          modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
  
      }
  }
  </code></pre>
  
  <p>I want to refactor out the bolded code, which obviously isn't that difficult but I can't seem to figure out how to pass in an Entity (Projection in the example above) so I can pass in any entity to set the properties.</p>
  
  <p>Something like;</p>
  
  <pre><code>private static void SetEntityPropertiesRequired&lt;TEntity&gt;(
                          DbModelBuilder modelBuilder,
                          TEntity entity)
      {
          //set all decimal properties in Projection Entity to be Required
          var decimalproperties = typeof (entity).GetProperties()
              .Where(p =&gt; p.PropertyType == typeof (decimal));
  
          foreach (var property in decimalproperties)
          {
              var lambda = BuildLambda&lt;entity, decimal&gt;(property);
              modelBuilder.Entity&lt;entity&gt;()
                  .Property(lambda)
                  .IsRequired();
          }
      }
  </code></pre>
  
  <p>and calling it like so;</p>
  
  <pre><code> SetEntityPropertiesRequired&lt;Projection&gt;(modelBuilder);
  </code></pre>
  
  <p>yields an error, method requires two parameters.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The answer, per SLaks answer in total is;</p>
  
  <pre><code>  private static void SetEntityPropertiesDecimalTypeToRequired&lt;TEntity&gt;(
                          DbModelBuilder modelBuilder) where TEntity : class
      {
          //set all decimal properties in Projection Entity to be Required
          var decimalproperties = typeof(TEntity).GetProperties()
              .Where(p =&gt; p.PropertyType == typeof(decimal));
  
          foreach (var property in decimalproperties)
          {
              var lambda = BuildLambda&lt;TEntity, decimal&gt;(property);
              modelBuilder.Entity&lt;TEntity&gt;()
                  .Property(lambda)
                  .IsRequired();
          }
      }
  </code></pre>
  
  <p>Called like this;</p>
  
  <pre><code>SetEntityPropertiesDecimalTypeToRequired&lt;Projection&gt;(modelBuilder);
  </code></pre>
  
  <p>A nice bonus would being able to pass in the property type(decimal, int, string, etc)! Anyone want to give that a try since all types arent the same, some are structs and some are classes</p>