【问题标题】:How to map an enum in a one-to-many relationship with NHibernate?如何将枚举映射为与 NHibernate 的一对多关系?
【发布时间】:2017-09-26 20:52:29
【问题描述】:

我有两个单独的表。

[users]   [ roles ] 
+-----+   +-------+
| id  |   |user_id|
+-----+   +-------+
          | value | <- [Represented by the enum]
          +-------+

还有他们的模型。

class User { int id; IList<Roles> Roles; }

enum Roles { Worker, Manager, Director }

如您所见,它很简单“一对多”。用户有很多角色。如何在 XML 中映射这些模型?

【问题讨论】:

  • 位掩码操作可能会解决这个问题。但在这种情况下,我需要更改现有的 API。

标签: c# .net nhibernate nhibernate-mapping


【解决方案1】:

你的映射怎么样? 你有没有尝试过这样的事情:

 HasMany(x => x.Roles)
   .Cascade.All()
   .Table("UserRoles")
   .Element("RolesEnum");

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但在寻找答案时将其作为第一个链接之一。似乎为 User 实体执行此操作(使用 FluentNhibernate):

    HasMany(x => x.Roles)
    .Cascade.All()
    .Table("roles")
    .KeyColumn("user_id")
    .Element("value", m => m.Type<EnumStringType<Roles>>());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多