开发者

Possible reasons for error: "The cast to value type 'Decimal' failed because the materialized value is null"

开发者 https://www.devze.com 2023-02-27 04:31 出处:网络
I\'m getting a report of errors from a production system. I don\'t have easy access to the production system. I will get all the data that I need for it in due course, but what I\'m trying to do is ex

I'm getting a report of errors from a production system. I don't have easy access to the production system. I will get all the data that I need for it in due course, but what I'm trying to do is expedite the process of resolution of this issue. I'm trying to eliminate as much guesswork as possible.

The error I'm getting this this:

Extended Properties: Exception - System.InvalidOperationException: The cast to value type 'Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

The code where the error is happening is this:

IQueryable<AccountStatusModel> result = from d in Context.Debtors
    where // Condition here
    select new AccountStatusModel
    {
         // All of the below are decimal in the AccountStatusModel
         // and all of them are decimal NOT NULL in the database
         NotYetDue = d.sometable.dbcurrent,
         DueThisMonth = d.sometable.dbprd1bal,
         Overdue = d.sometable.dbprd2bal
                + d.sometable.dbprd3bal
                + d.sometable.dbprd4bal
                + d.sometable.dbprd5bal
                + d.sometable.dbprd6bal,
         PaymentRecievedNotAllocated = d.sometable.dbunalloc,
         OutstandingOrders = d.dmoutordval ?? 0,
         TotalBalance = d.sometable.dbcurrent
                + d.sometable.dbprd1bal
                + d.sometable.dbprd2bal
                + d.sometable.dbprd3bal
                + d.sometable.dbprd4bal
                + d.sometable.dbprd5bal
                + d.sometable.dbprd6bal
                + d.sometable.dbunalloc
                + (d.dmoutordval ?? 0),
         CreditLimit = d.dmcredlim,
         LastPayment = d.dmvallastpaid,

         // Some other properties here
         // ....
    };

Before someone comments on the field names, assume that these are fake names. =) Note, that all the database fields being queried are NOT NULL.

I cannot reproduce this error in dev environment.

I think, that the reason this error happening in PROD, is because somehow, among the fields that are being queried, one is marked as NULL. I have no quick way of checking this, but I eventually will.

My question for now is this: Could there be any other reason for this error? Is it possible to get this error if the query is as above, and the fields are indeed NOT NULLs. (And why?) If it is possible could you p开发者_如何学编程lease provide an example how.

Please only answer if you are positive that you are right, i.e. I need something stronger, than "I think this is so".


So as I suspected in my comment to GregC answer, the problem was with a join. The schema turned out to be correct. However ever the query that I cited, can return nulls if "sometable" does not have a record for related "Debtors" record. I this case d.sometable.Anything will be null. Because L2E coalesces NULLS it does not complain that d.sometable is null simply returning null for d.sometable.Anything instead.

So this how and why.


Your query looks good. The key here is the word "materialized," which implies that the exception happened when hydrating a table cell with a DBNull in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消