2

I have a problem with insert decimal(10, 7) value into SQL Server

When I'm trying to add number from an ASP.NET MVC view, 51.0492263 into database

In debugger I have number = 51.0492263

But when I'm go to database, I have 51.0400000

Why I got this result?

Someone help please me.

Nazarii Iaremii
  • 133
  • 3
  • 8

1 Answers1

5

The default scale for Entity Framework, when using Code First, is 2. It's possible to use Code First with an existing database, but it's up to you to ensure that the database structure matches what EF thinks it should be.

Unfortunately, there isn't a ready-to-go attribute for specifying the scale. You'll have to use DbModelBuilder. In your context, you can put

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<Device>().Property(d => d.Latitude).HasPrecision(10, 7);
}
  • thx for answer, I have one more question, when Im read this info from database got same problem, in debugger correct values, but on page have Latitude 51.05 – Nazarii Iaremii Dec 28 '13 at 12:37
  • Hmm strange, Im delete @Html.DisplayFor(modelItem => item.Latitude) and use @item.Latitude and all works fine :) – Nazarii Iaremii Dec 28 '13 at 13:02
  • 1
    @NazarIaremii I'm not familiar with MVC, but I can imagine that it itself has some default formatting settings that get used with `DisplayFor`, that don't get used when you print the latitude directly. If that is correct, [this other question](http://stackoverflow.com/q/12067375/743382) may help you to get the formatting correct even with `DisplayFor`. –  Dec 28 '13 at 17:56