您的位置:首页 > 其它

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

2015-05-06 11:16 253 查看
通过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; }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐