通过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; }
}