Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4251570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:40:45+00:00 2026-05-21T04:40:45+00:00

I am using Entity Framework Code First to create a database table. My model

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:40:46+00:00Added an answer on May 21, 2026 at 4:40 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I'm trying to create a billing database with Entity Framework 4.3 using Code First
I'm using Entity Framework Code First. The class i'm trying to create contains two
When using Entity Framework Code First 4.3.1 it is possible to create relationships with
I am Using C# 2010 Entity framework code first If I have A class
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF).
I'm creating a database using entity framework code first and I'm having some issues
I'm trying to get Entity Framework Code First to create a database for me
I deleted a table in management studio, using code first entity framework shouldn't it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.