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

  • Home
  • SEARCH
  • 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

What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first
Using the Entity Framework 4. I have created a Code First database in the
I'm using Entity Framework code first to manage a database backstore for my users.
I have been using Entity Framework CTP with Code-First as in this tutorial by
How to declare a one to one relationship using Entity Framework 4 Code First
Is there a way in Entity Framework 4 (using CTP4 and Code First if
I'm working on an MVC site with Entity Framework Code First, using Ninject for
I'm building my application with Entity Framework (model first principle). I'm also using MS
I'm trying to implement the repository pattern using entity framework code first rc 1.
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My

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.