繁体   English   中英

EF5代码首次迁移:小数精度和小数位数

[英]EF5 code first migrations: decimal precision and scale

我正在尝试在现有的销售点信息亭应用程序中使用代码优先。 这是一个加油站应用程序,所以我需要使用小数点后三位数的货币。

我在迁移中使用以下代码来设置精度和比例:

CustomSqlGenerator.DropDefaultConstraint("Config", "DefaultTaxPerDollar", q => Sql(q));
    AlterColumn("Config", "DefaultTaxPerDollar", c => c.Decimal(nullable: false, precision: 19, scale: 4, defaultValue: 0.087m));

DropDefaultConstraint调用是这个bug的一种解决方法。我已经尝试删除它 - 在初始迁移中创建列而不是在以后更改它 - 无济于事。)

并且以适当的精度和比例创建列。 我可以使用SSMS正确输入值(即1.2345保存为1.2345)。 但是当通过模型保存值时,所有值都被截断 - 而不是舍入 - 到2个小数位(例如0.5555变为0.55)。

我试着用在OnModelCreating方法流利的API如图所示的第一件事, 在这里

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity()
        .Property(c => c.DefaultTaxPerDollar)
        .HasPrecision(19, 4);

    base.OnModelCreating(modelBuilder);
}

但是这产生The model backing the 'SalesDataStore' context has changed since the database was created. Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 ) The model backing the 'SalesDataStore' context has changed since the database was created. Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/?LinkId=238269 )

还尝试过:

  • OnModelCreating方法中包含modelBuilder.Conventions.Remove<DecimalPropertyConvention>() ,以完全删除OnModelCreating十进制约定。 与上述相同的例外
  • 从头开始构建最新的源代码 ,并在DecimalPropertyConvention类中将默认比例更改为4。 同样的例外。

映射定义了您的模型。 每次迁移都存储模型的压缩XML表示(也可能是哈希)。 当您更改映射中的任何内容(包括删除任何默认约定)时,还会更改映射及其哈希的最终XML表示形式。 EF使用这些哈希来检查模型是否更改 - 如果更改模型,则还必须进行新迁移才能使其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM