Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate question

Let's say you have two tables, "Users" and "UserRoles". Here's how the two tables are structured (table - columns):

Users - UserID (int)

UserRoles - UserID (int), Role (string)

What I want is for my "User" class in my domain to have an IList of roles. How do I construct my Fluent NHibernate mapping to achieve this?

like image 425
Kevin Pang Avatar asked Jan 11 '09 12:01

Kevin Pang


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate is another way of mapping or you can say it is an alternative to NHibernate's standard XML mapping files. Instead of writing XML (. hbm. xml files) documents.

Is NHibernate an ORM?

NHibernate is a port of Hibernate from Java, one of the oldest and most respected Object-Relational Mappers (ORMs).

What is the use of NHibernate?

NHibernate is Designed to serve as a persistence layer exclusively for the . Net framework based on Object-Relational Mapping Technique. A tool that creates a "virtual representation" of database objects within the code. Used to solve the problem of impedance mismatch between Class and relational databases and tables.


1 Answers

What you're looking for is a of a set of elements, which in standard hbm mapping is:

<set name="Roles" table="UserRoles">
  <key column="UserID" />
  <element column="Role" />
</set>

For Fluent NHibernate you can map this like so:

HasMany<string>(x => x.Roles)
  .AsElement("Role");

You may need to also specify the key name using WithKeyColumn(string).

like image 72
cicakman Avatar answered Sep 20 '22 17:09

cicakman