Greg Rotman
Greg Rotman

Reputation: 97

Parameter value is out of range

It seems EF is padding zeros to the value that I'm trying to save and then throwing an exception. Here is my code: Why is the total hours field with a value of 10.17 out of range of a decimal(6,5) value type and why is it padding this value with zeros? Saving this value directly using SQL Server Management Studio doesn't seem to be a problem, so it seems more like a problem with EF.

This is from a database first .edmx file. I've tried setting the scale and precision and setting to none. Neither have worked

 var rVal = new Data.FactPayrollHour()
                {
                    BereavementHours = 0.0M,
                    CostCenter = "MyCostCenter",
                    Department = "MyDepartment",
                    EmergencyHours = 0.0M,
                    HomeGroupCode = "MyHomeGroup",
                    PayrollEmployeeId = employees.FirstOrDefault().Id,
                    PayType = "MyPayType",
                    PunchDateKey = dateDims.FirstOrDefault().ID,
                    Schedule = "",
                    ServiceHours = 0.0M,
                    Shift = "",
                    ShortTermHours = 0.0M,
                    TimePolicy = "",
                    TotalHours = 10.17M,
                    UnpaidHours = 0.0M,
                    VacationHours = 0.0M
                };
                db.FactPayrollHours.Add(rVal);
                db.SaveChanges();

The exception message is: Parameter value '10.17000' is out of range.

Stack Trace:

   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()

Upvotes: 0

Views: 6861

Answers (1)

kkica
kkica

Reputation: 4104

If the data type is decimal(6,5) you can use up to 6 total decimal digits, 5 of which will be reserved for after the decimal point (that is why you have additional 0-es after the decimal point).

That leaves you with 1 digit before (on the left of) the decimal point. And you are using 2. That is why you have the error.

Upvotes: 5

Related Questions