使用 NHibernate/Castle ActiveRecord 将枚举映射到数据库

发布于 2024-09-04 06:55:37 字数 859 浏览 6 评论 0原文

还有其他一些关于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喵星人汪星人 2024-09-11 06:55:37

编写一个覆盖 SqlType 的通用 EnumStringType,然后应用它:

public class EnumMapper<T> : NHibernate.Type.EnumStringType<T>
{
   public EnumMapper()
   {
   }

   public override NHibernate.SqlTypes.SqlType SqlType
   {
      get
      {
         return new NHibernate.SqlTypes.SqlType(DbType.Object);
      }
   }
}

应用它:

[Property(NotNull = true, 
          ColumnType = "MyNamespace1.EnumMapper`1[MyNamespace2.OrderState, MyAssembly2], MyNamespace1")]
public OrderState State {get;set;}

Write a generic EnumStringType that overrides SqlType, then apply it:

public class EnumMapper<T> : NHibernate.Type.EnumStringType<T>
{
   public EnumMapper()
   {
   }

   public override NHibernate.SqlTypes.SqlType SqlType
   {
      get
      {
         return new NHibernate.SqlTypes.SqlType(DbType.Object);
      }
   }
}

apply it:

[Property(NotNull = true, 
          ColumnType = "MyNamespace1.EnumMapper`1[MyNamespace2.OrderState, MyAssembly2], MyNamespace1")]
public OrderState State {get;set;}
凯凯我们等你回来 2024-09-11 06:55:37

除非我遗漏了什么,否则你可以这样做:

public enum ExampleEnum
{ 
    Value1,
    Value2
}

[ActiveRecord]
public class ExampleClass 
{
    [PrimaryKey]
    public int ID { get; set; }

    [Property]
    public ExampleEnum Example { get; set; }
}

似乎对我来说非常有效。

Unless I'm missing something, you can just do this:

public enum ExampleEnum
{ 
    Value1,
    Value2
}

[ActiveRecord]
public class ExampleClass 
{
    [PrimaryKey]
    public int ID { get; set; }

    [Property]
    public ExampleEnum Example { get; set; }
}

Seems to work perfectly for me.

岁月打碎记忆 2024-09-11 06:55:37
ColumnType = typeof(EnumStringType<OrderState>).AssemblyQualifiedName
ColumnType = typeof(EnumStringType<OrderState>).AssemblyQualifiedName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文