使用 NHibernate/Castle ActiveRecord 将枚举映射到数据库
还有其他一些关于使用 ActiveRecord 将枚举映射到数据库的帖子,但没有一个回答我的问题。我有一个名为 OrderState 的枚举:
public enum OrderState {InQueue, Ordered, Error, Cancelled}
我在表中有以下属性:
[Property(NotNull = true, SqlType = "orderstate", ColumnType = "DB.EnumMapper, WebSite")]
public OrderState State
{
get { return state; }
set { state = value; }
}
我有以下类型类:
public class EnumMapper : NHibernate.Type.EnumStringType<OrderState>
{
public EnumMapper()
{
}
public override NHibernate.SqlTypes.SqlType SqlType
{
get
{
return new NHibernate.SqlTypes.SqlType(DbType.Object);
}
}
}
现在这实际上按照我想要的方式工作,但问题是我有大量枚举,但我不想为其中每一个创建一个 EnumMapper 类。是否有某种方法可以告诉 ActiveRecord 对任何枚举使用 DbType.Object?它似乎要么想要一个整数,要么想要一个字符串,但没有别的。在过去的两个小时里,这个让我发疯了..
迈克
There's a few other posts on mapping Enums to the DB with ActiveRecord, but none of them answer my question. I have an enum called OrderState:
public enum OrderState {InQueue, Ordered, Error, Cancelled}
And I have the following property on the table:
[Property(NotNull = true, SqlType = "orderstate", ColumnType = "DB.EnumMapper, WebSite")]
public OrderState State
{
get { return state; }
set { state = value; }
}
And I have the following type class:
public class EnumMapper : NHibernate.Type.EnumStringType<OrderState>
{
public EnumMapper()
{
}
public override NHibernate.SqlTypes.SqlType SqlType
{
get
{
return new NHibernate.SqlTypes.SqlType(DbType.Object);
}
}
}
Now this actually works the way I want, but the problem is I have tons of enums and I don't want to create a EnumMapper class for each one of them. Isn't there some way to just tell ActiveRecord to use DbType.Object for any enum? It seems to either want to be an integer or a string, but nothing else. This one's been driving me crazy for the last 2 hours..
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编写一个覆盖 SqlType 的通用 EnumStringType,然后应用它:
应用它:
Write a generic EnumStringType that overrides SqlType, then apply it:
apply it:
除非我遗漏了什么,否则你可以这样做:
似乎对我来说非常有效。
Unless I'm missing something, you can just do this:
Seems to work perfectly for me.