asp.net mvc - Decimal Precision Lost when saved to DB, C#. I am using Entity Framework -
this question has answer here:
- decimal precision , scale in ef code first 13 answers
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 latitude , longitude. format in sql server db decimal(11, 6). when give values latitude = 41.32056 , longitude = 19.805542 in create from, debug , see model 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 value stored in db latitude = 41.320000 , longitude = 19.800000. should latitude = 41.32056 , longitude = 19.805542. what missing.
my dbcontext class looks 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 have change dbcontext class?
update regards update - need add following applicationdbcontext:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<hotel>().property(x => x.longitude).hasprecision(11, 6); }
look here
Comments
Post a Comment