Mapping Unsigned Integer and Long Types in Entity Framework
Entity Framework's out-of-the-box mapping for long data types is sufficient for representing signed long values. However, when dealing with unsigned long (ulong) types, things get a bit more complicated. MySQL's EF provider skips ulong data types by default.
Solution for ulong
In older versions of Entity Framework, unsigned data types were not supported. To work around this limitation for ulong columns, a workaround is to store the value in a supported long data type and cast it to ulong when needed.
To implement this solution:
// Avoid modifying the following directly.
// Used as a database column only.
public long __MyVariable { get; set; }
// Access/modify this variable instead.
// Tell EF not to map this field to a Db table
[NotMapped]
public ulong MyVariable
{
get
{
unchecked
{
return (ulong)__MyVariable;
}
}
set
{
unchecked
{
__MyVariable = (long)value;
}
}
}
Note that the casting operations are marked as unchecked to prevent any potential overflow exceptions.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3