0

I have a decimal property in Product entity:

public class Product
{
   public Guid Id { get; set; }
   public decimal Price { get; set; }
}

I want to config precision in model mapping:

   class Context : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public Context() : base("server=localhost;database=myDb2;trusted_connection=true;")
        {
            /* Database.Delete();
             Database.Create(); */
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().HasKey(e => new { e.Id });
            modelBuilder.Entity<Product>().Property(s => s.Price).HasPrecision(29, 10);
        }
    }

Now I'm trying to save decimal that has 19 lengths:

using (Context context = new Context())
        {
            var product = new Product();
            product.Id = Guid.NewGuid();
            product.Price = 9999999999999999999M;
            context.Products.Add(product);
            context.SaveChanges();
        }

It is throwing exception:

System.Data.Entity.Infrastructure.DbUpdateException HResult=0x80131501 Message=An error occurred while updating the entries. See the inner exception for details. Source=EntityFramework StackTrace: at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at ConsoleApp11.Program.Main(String[] args) in C:\Users\dilshodk\source\repos\ConsoleApp11\ConsoleApp11\Program.cs:line 22

Inner Exception 1: UpdateException: An error occurred while updating the entries. See the inner exception for details.

Inner Exception 2: OverflowException: Conversion overflows.

When I try to insert value from SQL query it works:

 Insert into Products values (NEWID(),9999999999999999999)

Why it is not working from EF and how can I solve it?

Dilshod K
  • 2,924
  • 1
  • 13
  • 46

1 Answers1

0

he he he. :o) yes, you have a problem, you see - you defined it as (database type) decimal(29,10), that means 29 places, one for the dot, 10 for the digits (e.g. 0.0123456789 is valid, but 0.00123456789 will be written as 0.0012345678), leaving 18 places for the wholes (e.g. 123456789012345678 is fine but 19 digits of 9 is too big). Just set it up as HasPercision(30,10) or HasPercision(29,5) and you're good to go.

LongChalk
  • 783
  • 8
  • 13
  • HasPercision(30,10) it is still throwing exception – Dilshod K Feb 11 '20 at 08:27
  • Also, I tried HasPercision(31,10), HasPercision(32,10), HasPercision(33,10) and even HasPercision(38,10) – Dilshod K Feb 11 '20 at 08:33
  • I am having same challenge too. I have on this for two days now. Any help guys??? – Cyril Ikelie Jul 12 '21 at 19:47
  • Did you try HasPercision(50,2) ? Did you define it in the database as decimal(50,2) as well? just saying, this is one of those things where you have to be very explicit about what you want to record in the database. I've never seen anything that measures 50 digiits in practice. Even the size of the sun is not 50 digits if you measure it in tons. – LongChalk Jul 23 '21 at 06:59