0

I will likely have dozens of decimal properties in my model, and I set the precision in OnModelCreating like so:

modelBuilder.Entity<Models.MyModel>()
                    .Property(x => x.MyProp)
                    .HasPrecision(18, 2);

But it's already getting out of hand with the length, so I was wondering if there was a way to iterate through every decimal in my context and set the precision (18,2)?

Bonnie
  • 41
  • 4

2 Answers2

0

Fluent Validation. A popular .NET library for building strongly-typed validation rules. Checkout this and hope this will resolve your problem.

  • 1
    What OP is asking for is different from validation. `HasPrecision` is used so entity framework knows what data type to give to the column in whatever database is used behind it. –  Jul 15 '21 at 09:24
0

EF allows you to inherit from EntityTypeConfiguration and use it to make your own configurations for each entity. Then add the EntityTypeConfiguration-derived class to the fluent set.

For example: book configuration class

public class BookConfig : EntityTypeConfiguration<Book>
{
   public BookConfig()
   {
      Property(p => p.bookNo).HasPrecision(18, 2);
      Property(p => p.BookPrice).HasPrecision(18, 2);
   }
}

Now introduce it to the fluent library

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);
   modelBuilder.Configurations.Add(new BookConfig());
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17