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?
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.
NHibernate is a port of Hibernate from Java, one of the oldest and most respected Object-Relational Mappers (ORMs).
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With