Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal Precision Lost when saved to DB, C#. I am using Entity Framework [duplicate]

My model

public class Hotel
{
    public int Id { get; set; }
    [Required]
    [Display(Name="Hotel Name")]
    public string HotelName {get;set;}
    [Required]
    public string Address { get; set; }
    [Required]
    [DisplayFormat(DataFormatString = "{0:N6}", ApplyFormatInEditMode = true)]
    [RegularExpression(@"\d{1,10}(\.\d{1,6})", ErrorMessage = "Invalid Latitude")]
    public Decimal Latitude { get; set; }
    [Required]
    [DisplayFormat(DataFormatString = "{0:N6}", ApplyFormatInEditMode = true)]
    [RegularExpression(@"\d{1,10}(\.\d{1,6})", ErrorMessage = "Invalid Longitude")]
    public Decimal Longitude { get; set; }
    [Required]
    [RegularExpression(@"\d{10,20}", ErrorMessage = "Invalid Number")]    
    public string Telephone { get; set; }
    [Required]
    [EmailAddress]
    public string Email { get; set; }

}

The problem is with Latitude and Longitude. Their format in SQL Server DB is decimal(11, 6). So when i give the values Latitude = 41.32056 and Longitude = 19.805542 in the create from, i debug and see that the model is built correctly

[HttpPost]
public ActionResult Create(Hotel hotel)
{
    try
    {
        // TODO: Add insert logic here
        if (ModelState.IsValid)
        {
            db.Hotels.Add(hotel);
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

but the value stored in DB is Latitude = 41.320000 and Longitude = 19.800000. It should be Latitude = 41.32056 and Longitude = 19.805542. What am I missing.

My DbContext class looks like this

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }        


        public DbSet<Hotel> Hotels { get; set; }
        public DbSet<Notification> Notifications { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<Booking> Bookings { get; set; }
        public DbSet<Audit> Audit { get; set; }      

    }

I have never used DbModelBuilder. Do I have to change my DbContext class?

like image 627
Jona Avatar asked Jan 27 '16 12:01

Jona


2 Answers

UPDATE With regards to your update - you need to add the following to your ApplicationDbContext:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Hotel>().Property(x => x.Longitude).HasPrecision(11, 6);
    }

Look here

Decimal precision and scale in EF Code First

like image 186
berliner Avatar answered Nov 11 '22 09:11

berliner


Try adding a mapping:

modelBuilder.Entity<Hotel>().Property(x => x.Longitude).HasPrecision(11, 6);
like image 5
Martin Staufcik Avatar answered Nov 11 '22 10:11

Martin Staufcik