Entity Framework Code First truncating my decimals

decimal ef-code-first entity-framework-6 geospatial rounding-error

Popular Answer

For the implementation of the SqlClient provider for SQL Server, SqlProviderServices, EF includes a particular property called TruncateDecimalsToScale. Since true is the default value, you might want to modify it to false. For instance:

public class DbContextConfiguration : DbConfiguration
    {
        public DbContextConfiguration()
        {
            var now = SqlProviderServices.Instance;
            SqlProviderServices.TruncateDecimalsToScale = false;
            this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }

    [DbConfigurationType(typeof(DbContextConfiguration))]
    public class MyContext : DbContext
    { ... }

For further information, call https://msdn.microsoft.com/en-us/library/system.data.entity.sqlserver.sqlproviderservices.truncatedecimalstoscale%28v=vs.113%29.aspx.

18
8/23/2016 9:45:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow