Recently I encountered following problem in C#.
1 2 |
System.InvalidOperationException was unhandled by user code Message: The 'Id' property on 'SomeDAO' could not be set to a 'System.Decimal' value. You must set this property to a non-null value of type 'System.Int64'. |
This error is pretty obvious in certain context, but if you don’t read it carefully enough, in some situations you might get little bit confused just as I was in first moment.
TLDR:
You are assigning incompatible type. In case above, the attempt was to assign 128bit System.Decimal
variable into 64bit Long
variable.
Why did you missinterpreted compiler message and got to this page
In my case, I had some class SomeDao
with declared property Id
of type Long
. The error was popping out when I tried to create this Dao from database by ORM.
So when I first saw the error, I read “The ‘Id’ property on ‘SomeDao’ could not be …. You must set this property to non-null value of type 'System.Int64'
” …ahhh…wait what ??? But the Id
on SomeDao
is ALREADY Long / System.Int64
. What do you want C# ?
The problem was I didn’t read the sentence carefully enough until it very end and my eyes just jumped to that last sentence.
Obviously, one need’s to also realize the meaning of word ‘set’ in that sentence. It means you must set, AKA assign value, of type Int64
. This is not reffering going to the code and changing the type of property Id in SomeDao
.
So the only problem is the type you are trying to assign to this property. In my case, entity framework loaded value of Id from database of type System.Decimal
and trying set SomeDao.id
to this value. Which is not compatible, since Decimal
is 128bit float.
Oh wait ! Before you judge me … you must be wondering why on Earth I would want to load Id into floating point type, it’s because in JIRA database is using 128bit type for ID’s.
Well, JIRA needs a lot of IDs …