解決使用DbContext儲存Decimal資料時總是保留小數位2位問題

NO IMAGE

通過System.Data.Entity.DbContext保留Decimal型別資料時,預設只保留小數位2位。要解決該問題,可以通過在OnModelCreating事件中新增相應程式碼即可,具體參考如下程式碼中將shop.Longitude設定為小數位20位:

public class UserDbContext : System.Data.Entity.DbContext
{
public UserDbContext()
: base("MyContext")
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Shop>().Property(shop => shop.Longitude).HasPrecision(30, 20);
modelBuilder.Entity<Shop>().Property(shop => shop.Latitude).HasPrecision(30, 20);
modelBuilder.Entity<User>().Property(user => user.ArtificerLatitude).HasPrecision(30, 20);
modelBuilder.Entity<User>().Property(user => user.ArtificerLongitude).HasPrecision(30, 20);
} 
public DbSet<User> Users { get; set; }
public DbSet<Shop> Shops { get; set; }
}