Il mio modelloPrecisione decimale persa se salvata in DB, C#. Sto usando Entity Framework
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; }
}
Il problema è con latitudine e longitudine. Il loro formato in SQL Server DB è decimale (11, 6). Così, quando mi danno i valori di latitudine = 41,32,056 mila e longitudine = 19,805,542 mila nel creare dal, il debug e vedere che il modello è costruito in modo corretto
[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();
}
}
ma il valore memorizzato nel DB è Latitudine = 41,320 mila e longitudine = 19,800 mila. Dovrebbe essere Latitude = 41.32056 e Longitudine = 19.805542. Cosa mi manca.
classe mia DbContext assomiglia a questo
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; }
}
non ho mai usato DbModelBuilder. Devo cambiare la mia classe DbContext?
puoi pubblicare il codice del tuo modellista? e qual è il tipo di colonna nel db? – DevilSuichiro
http://stackoverflow.com/questions/3504660/decimal-precision-and-scale-in-ef-code-first –