2016-01-27 6 views
5

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?

+0

puoi pubblicare il codice del tuo modellista? e qual è il tipo di colonna nel db? – DevilSuichiro

+4

http://stackoverflow.com/questions/3504660/decimal-precision-and-scale-in-ef-code-first –

risposta

3

UPDATE Per quanto riguarda l'aggiornamento - è necessario aggiungere quanto segue al ApplicationDbContext:

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

Guardate qui

Decimal precision and scale in EF Code First

+3

questa non dovrebbe essere una risposta ma un flag di domanda duplicato. – user1666620

+0

Grazie mille. Dopo averlo aggiunto a ApplicationDbContext, ho eseguito una migrazione dalla console di Package Manager con il comando Update-Database -Force. Grazie ancora. – user1234

4

Prova ad aggiungere una mappatura:

modelBuilder.Entity<Hotel>().Property(x => x.Longitude).HasPrecision(11, 6); 
+0

Modifica la mia domanda – user1234

Problemi correlati