I have an enum AccountType.
Currently, in my Account class I have second thing:
public class Account
{
public virtual long AccountId { get; set; }
public virtual int AccountTypeId { get; set; }
public virtual AccountType AccountType
{
get
{
return (AccountType)AccountTypeId;
}
set
{
AccountTypeId = (int)value;
}
}
}
and in my *.hbm.xml I have
<class name="Account" table="tAccount">
<id name="Id">
<column name="AccountId" sql-type="bigint"/>
<generator class="assigned"/>
</id>
<property name="AccountTypeId" column="AccountType"/>
</class>
Works fine BUT, now I need to change column AccountType from int to string, and as a result, old code wouldn't work any more.
So, basically, instead of int, I need to take string from database and get enum from it.
How it's supposed to be done?
PS Sorry for my bad English.
As already suggested by @Reniuz by using Enum.Parse
or better Enum.TryParse
:
public virtual AccountType? AccountType
{
get
{
AccountType type;
if (Enum.TryParse(AccountTypeString, out type))
return type
return null;
}
set
{
AccountTypeString = String.IsNullOrWhitespace(value) ? null : value.ToString();
}
}
Depending on how often this is called you possibly should somehow "cache" the parsed value.
UPDATE: I changed it to a nullable as per request.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.