quip
quip

Reputation: 3694

nHibernate code mapping for enum collection

I'm getting started with nHibernate code mapping and I'm stuck on mapping a collection of enums.

(Note, this question is similar to Mapping collection of enum in NHibernate but different in that I want to map using code mappings.)

I have an entity "Role" which has a collection of "Permissions", which Permissions is an enum.

The old xml mapping for this is:

<set name="Permissions" cascade="none" table="role_permissions">
  <key column="role_id" />
  <element column="permission_id" type="MyApp.Permissions, MyApp" />
</set>

And I'm trying to map in code like this, but I get an exception:

Set(x => x.PermissionCollection, m => { },
  r => r.Element(e =>
  {
    e.Column("permission_id");
    e.Type<Permissions>();
  }));

Exception thrown is

Expected type implementing IUserType or IType.
Parameter name: persistentType

Upvotes: 0

Views: 930

Answers (1)

hival
hival

Reputation: 675

Try this

Set(x => x.PermissionCollection, m =>
{
    m.Key(km => km.Column("role_id"));
    m.Table("role_permissions");
},
r => r.Element(e => e.Column("permission_id")));

Upvotes: 1

Related Questions