0

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
Community
  • 1
  • 1
quip
  • 3,666
  • 3
  • 32
  • 45

1 Answers1

1

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")));
hival
  • 675
  • 4
  • 5
  • 1
    Hmm, that got me past the initial configuration, but when trying to use the Role entity I'm getting an "Invalid Cast" exception. It might be a different problem, I'm not sure which property is causing the cast exception. – quip Feb 14 '12 at 19:23
  • Could you provide your Role class. – hival Feb 14 '12 at 19:28
  • In my test app this mapping persists and loads entity without any errors. Interestingly, what code throws this exception? – hival Feb 14 '12 at 19:41
  • This is strange: when I try the app again, i get a different exception. This time during the mapping configuration I get "Could not determine type for: MyApp.Role, MyApp, for columns: NHibernate.Mapping.Column(id)". I think you've given me the answer to my first problem, but looks like I've got some more work to do. – quip Feb 14 '12 at 19:46
  • It ended up being my version column - my code had it as "long?" but the configuration must have been trying to use an int. – quip Feb 14 '12 at 20:39