[英]How to map tinyint to c# byte using entityframework code first migration
I am trying to map tinyint column to byte property in c#How to achieve this efficiently. 我正在尝试将tinyint列映射到c#中的byte属性,如何有效地实现这一点。 I want to store the values null, 0 and 1 to the database column. 我想将值null,0和1存储到数据库列中。 Please find below my code. 请在下面找到我的代码。 Can anyone help me whether the following is the right approach? 任何人都可以帮助我以下方法是否正确?
public enum TriState : byte
{
Null = 0,
True = 1,
False =2
}
[NotMapped]
public TriState AuthorisationStatus { get; set; }
[Column("AuthorisationStatus")]
public byte AuthorisationStatusByte {
get
{
return Convert.ToByte(AuthorisationStatus.ToString());
}
private set
{
AuthorisationStatus = EnumExtensions.ParseEnum<TriState>(value);
}
}
public static T ParseEnum<T>(byte value)
{
return (T)Enum.Parse(typeof(T), value.ToString(), true);
}
Thanks 谢谢
There's no need to go via a string at all. 根本不需要通过字符串。 Just use the explicit conversions: 只需使用显式转换:
[Column("AuthorisationStatus")]
public byte AuthorisationStatusByte
{
get { return (byte) AuthorisationStatus; }
set { AuthorisationStatus = (TriState) value; }
}
(I'm assuming the column attribute is correct - I don't know about that side of things.) (我假设column属性是正确的-我对此一无所知。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.