Gre*_*man 0 c# entity-framework-6
看起来 EF 正在将零填充到我试图保存的值,然后引发异常。这是我的代码:为什么值为 10.17 的总小时数字段超出了小数(6,5)值类型的范围以及为什么它用零填充该值?直接使用SQL Server Management Studio保存这个值似乎没有问题,所以看起来更像是EF的问题。
这是来自数据库第一个 .edmx 文件。我尝试过将比例和精度设置为无。两者都没有工作
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();
Run Code Online (Sandbox Code Playgroud)
异常消息是:Parameter value '10.17000' is out of range.
堆栈跟踪:
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()
Run Code Online (Sandbox Code Playgroud)
如果数据类型是,decimal(6,5)您最多可以使用 6 位十进制数字,其中 5 位将保留在小数点后面(这就是为什么小数点后面有额外的 0)。
这样一来,小数点前(左边)就剩下 1 位数字了。而您正在使用 2。这就是您出现错误的原因。