解决使用DbContext保存Decimal数据时总是保留小数位2位问题

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

不是每个人都一定快乐,不是每种痛都一定要述说。

解决使用DbContext保存Decimal数据时总是保留小数位2位问题

相关文章:

你感兴趣的文章:

标签云: