1

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?

sfgroups
  • 18,151
  • 28
  • 132
  • 204

2 Answers2

4

This should work for you - using reflection to get all the properties of type decimal in 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.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var properties = typeof(Envelopes).GetProperties()
                                      .Where(p => p.PropertyType == typeof(decimal));

    foreach (var property in properties)
    {
        var lambda = BuildLambda<Envelopes, decimal>(property);
        modelBuilder.Entity<Envelopes>()
                    .Property(lambda)
                    .HasPrecision(18, 2);
    }
}

static Expression<Func<T, U>> BuildLambda<T,U>(PropertyInfo property)
{
    var param = Expression.Parameter(typeof(T), "p");
    MemberExpression memberExpression = Expression.Property(param, property);
    var lambda = Expression.Lambda<Func<T, U>>(memberExpression, param);
    return lambda;
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • thanks for the response, this code not compiling its giving error on this line `var lambda = BuildLambda(property);` error message not able resolve **Type1**. Do I need to use (import) some library? – sfgroups Apr 09 '11 at 02:25
  • Where can I learn how the BuildLambda method works line by line? This confuses me. – damccull Mar 22 '16 at 20:30
0

model creation with some reflection

var entityTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass && t.Namespace == "Namespace.Enitites");
foreach (var type in entityTypes)
{
    foreach (var property in type.GetProperties().Where(p => p.PropertyType == typeof(decimal)))
    {
        var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(type).Invoke(modelBuilder, null);
        var param = Expression.Parameter(type, "c");
        var lambdaExpression = Expression.Lambda(Expression.Property(param, property), true, new[] { param });

        var items = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property" && p.ReturnType == typeof(DecimalPropertyConfiguration)).ToArray();
        if (items.Length <= 0)
            continue;

        MethodInfo methodInfo;
        if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
            methodInfo = items[1];
        else
            methodInfo = items[0];

        var decimalConfig = methodInfo.Invoke(entityConfig, new[] { lambdaExpression }) as DecimalPropertyConfiguration;
        if (decimalConfig != null)
            decimalConfig.HasPrecision(19, 4);
    }
}

or

modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(19, 4));
RTK
  • 223
  • 2
  • 5