title: Loop/reflect through all properties in all EF Models to set Column Type
body: <p>My client has a standard of storing SQL Server decimals with a decimal(13,4) specification. As a result, in a very large and still-growing schema, I have nearly a hundred statements like these:</p>
  
  <pre><code>builder.Entity&lt;MyObject&gt;()
      .Property(x =&gt; x.MyField1)
      .ForSqlServerHasColumnType("decimal(13,4)");
  builder.Entity&lt;MyObject&gt;()
      .Property(x =&gt; x.MyField2)
      .ForSqlServerHasColumnType("decimal(13,4)");
  builder.Entity&lt;MyObject2&gt;()
      .Property(x =&gt; x.MyField1)
      .ForSqlServerHasColumnType("decimal(13,4)");
  </code></pre>
  
  <p>If there is a feature where I can tell EF directly that all decimals should be decimal(13,4) by default, I would like to use that. If not, can I use reflection to loop through every object/property in the model so I can do this in a couple statements? </p>
  
  <p>Something like:</p>
  
  <pre><code>foreach(var efObj in EntityFrameWorkObjects)
  {
      foreach (var objProperty in efObj)
      {
          if (objProperty is decimal || objProperty is decimal?)
          {
              builder.Entity&lt;efObj&gt;()
                  .Property(x =&gt; x.efObj)
                  .ForSqlServerHasColumnType("decimal(13,4)");
          }
      }
  }
  </code></pre>
  
  <p>Reflection seems like a great way to go, because then I can implement some of our other conventions where, if an object has a Name and Description, the Name is required and limited to 256 chars.</p>
  
  <p><strong>Update:</strong>
  I followed the link in Ivan's comment and adapted it to this, which works for me:</p>
  
  <pre><code>foreach (var p in builder.Model
      .GetEntityTypes()
      .SelectMany(t =&gt; t.GetProperties())
      .Where(p =&gt; 
          p.ClrType == typeof(decimal) ||
          p.ClrType == typeof(decimal?)))
  {
      p.SqlServer().ColumnType = "decimal(13,4)";
  }
  </code></pre>
  
  <p>Soon after, he provided a full answer, which I changed slightly to work with both decimal and nullable decimal:</p>
  
  <pre><code>foreach (var pb in builder.Model
      .GetEntityTypes()
      .SelectMany(t =&gt; t.GetProperties())
      .Where(p =&gt; 
          p.ClrType == typeof(decimal) ||
          p.ClrType == typeof(decimal?))
      .Select(p =&gt; 
          builder.Entity(p.DeclaringEntityType.ClrType)
              .Property(p.Name)))
  {
      pb.ForSqlServerHasColumnType("decimal(13,4)");
  }
  </code></pre>
  
  <p>Both approaches work!</p>
  
  <p><strong>Update 2:</strong> I had to have my objects declared as DbSet&lt;> in the context for the above to work. This didn't seem to be required when I was setting properties line by line.</p>