事实证明,实体框架不支持unsigned
数据类型。对于uint
列,可以只将值存储在更大范围内的带符号数据类型中(即along
)。那ulong
列呢?通用解决方案不适用于我,因为没有EF支持的带符号数据类型可以容纳且ulong
不会溢出。
经过一番思考,我找到了解决此问题的简单方法:只需将数据存储为受支持的long
类型,并ulong
在访问时将其强制转换为即可。您可能会想:“但是,等等,ulong的最大值> long的最大值!” 您仍然可以长时间存储ulong的字节,然后在需要时将其转换回ulong,因为它们都有8个字节。这将允许您通过EF将ulong变量保存到数据库。
// 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;
}
}
}
强制转换是unchecked
为了防止溢出异常。
希望这对某人有帮助。