I am using Entity Framework Code First to create a database table. My model class has ten decimal fields. Currently I am setting the field property like this in the OnModelCreating method:
modelBuilder.Entity<Envelopes>().Property(p => p.cell_1_1).HasPrecision(18, 2);
Since I have ten fields I am thinking of using a for loop to set this precision property such as the following code:
for( int i = 1; i <= 10; i++ ) {
modelBuilder.Entity<Envelopes>()
.Property(p => p.Equals("cell_1_"+i ))
.HasPrecision(18, 2);
}
However the above code is giving a me a syntax error.
Is it possible to set the precision value like this?
This should work for you – using reflection to get all the properties of type
decimalin your entity, then building an expression tree for the property access and finally using the property access lambda to set the precision to the desired values.