ASP.NET MVC 4将3个小数位值保存到SQL Server 2005 [英] ASP.NET MVC 4 save 3 decimal place value to SQL Server 2005

查看:54
本文介绍了ASP.NET MVC 4将3个小数位值保存到SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在SQL Server 2005中设置小数点后三位.我只能从模型部分进行设置.但是在sql server中,设置Salary属性的数据类型为decimal.

I have to set 3 decimal place in SQL Server 2005.i can set it from only model part .But In sql server set datatype of Salary property is decimal.

 When I set like

         public decimal Salary { get; set; }

它会像21000.000而不是21000这样保存到数据库.如果我输入30000.75,那么它将保存到30000.750.

It would be saved to the database like 21000.000 not 21000. If I put 30000.75, then it would be saved to 30000.750.

推荐答案

我也陷入了同样的情况,并最终找到了解决方案.您可以覆盖DbContext.OnModelCreating()方法并配置精度

I also got stuck in the same situation and finally found the solution. You can override the DbContext.OnModelCreating() method and configure the precision


public class ApplicationDbContext : IdentityDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity().Property(c => c.AssociationFee).HasPrecision(18, 3);
    }
}

希望这会有所帮助

这篇关于ASP.NET MVC 4将3个小数位值保存到SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆