Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Decimal Trimmed after 2 digits added Zeros while store to sql server

I trying to store the Latitude and Longitude to my Database.
I have My column type in DB as

[Latitude]              NUMERIC (12, 8)                NULL,
[Longitude]             NUMERIC (12, 8)                NULL,

In C# Model my Properties are

    [Column(TypeName = "numeric")]
    [Range(-9999.99999999, 9999.99999999)]
    public decimal? Latitude { get; set; }


    [Column(TypeName = "numeric")]
    [Range(-9999.99999999, 9999.99999999)]
    public decimal? Longitude { get; set; }

I want to store this Latitude and Longitude value to my Database table

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am doing wrong here please help

decimal lats = decimal.Parse("56.12345678");
        decimal longs = decimal.Parse("10.18732210");

        context.test.Add(new test
        {
            Latitude = lats,
            Longitude = longs
        });
        context.SaveChanges();

it stores Like in DB
Like This

Latitude: 56.12000000  and Longitude : 10.18000000

I want the Same Value to be stored in DB
"56.12345678" and "10.18732210";

>Solution :

You probably need to specify it also in your OnModelCreating like this:

modelBuilder.Entity<Test>().Property(a => a.Latitude).HasPrecision(18, 9);
modelBuilder.Entity<Test>().Property(a => a.Longitude).HasPrecision(18, 9);

Just in case you need more advanced features and you are using EF Core take a look at:
https://docs.microsoft.com/en-us/ef/core/modeling/spatial

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading