码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-05-06 13:20:40      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:dbcontext   decimal   小数位   精度   

通过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位问题

标签:dbcontext   decimal   小数位   精度   

原文地址:http://blog.csdn.net/mole/article/details/45533483

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!