I have the following entity
public class Employee
{
public virtual int Id {get;set;}
public virtual ISet<Hour> XboxBreakHours{get;set}
public virtual ISet<Hour> CoffeeBreakHours {get;set}
}
public class Hour
{
public DateTime Time {get;set;}
}
(What I want to do here is store information that employee A plays Xbox everyday let’s say at 9:00 13:30 and has a coffee break everyday at 7:00 12:30 18:00) – I am not sure if my approach is valid at all here.
The question is how should my (ideally fluent) mappings look like here? It is not necessary (from my point of view) for Hour class to have Id or be accessible from some kind of repository.
Depending on how you want to do it, you either need to map your collection as an element mapping or as a component collection (that’s
<element>and<composite-element>in NHibernate terms). The former will need anIUserTypedefining, while the latter is for if you’re going to have yourHourclass have more than one property.If you’re sticking with a single property, you’ll need to define an
IUserTypeso NHibernate knows how to translate to and from your type to your database. Once you’ve done that, you can map it with Fluent NHibernate like so:That specifies that your collection is stored in a table with a column called
valuecontaining the actual values. TheCustomTypecall is what tells NHibernate to use theIUserTypefor this collection.If you’re going to have multiple properties in your
Hourclass, then you need to do the following (note: this is actually very similar to doing aComponentmapping).