You can use the following FNH mapping signature to map collections of value types.
HasMany(x => x.Collection) .Table("TableName") .KeyColumn("KeyColumnName") .Element("ValueColumnName");
Where:
- Collection: a set of value types (may be
enum , because it will display as int). - TableName: the name of the table in which your collection values will be stored.
- KeyColumnName: the name of the column in which the key value for the parent will be stored.
- ValueColumnName: the name of the column in which the actual value will be stored.
Let's look at an example of matching multiple sets of value types.
public enum EnumType { Value1, Value2, Value3 } public class Entity { /// <summary> /// Primary key /// </summary> public virtual int Id { get; set; } /// <summary> /// Collection of strings /// </summary> public virtual IList<string> StringCollection { get; set; } /// <summary> /// Collection of enums /// </summary> public virtual IList<EnumType> EnumCollection { get; set; } /// <summary> /// Collection of dates/times /// </summary> public virtual IList<DateTime> DateTimeCollection { get; set; } } public class EntityMap : ClassMap<Entity> { public EntityMap() { // Map primary key. Id(x => x.Id); // Map value collections HasMany(x => x.StringCollection) .Table("Entity_String") .KeyColumn("EntityId") .Element("String"); HasMany(x => x.EnumCollection) .Table("Entity_Enum") .KeyColumn("EntityId") .Element("Enum"); HasMany(x => x.DateTimeCollection) .Table("Entity_DateTime") .KeyColumn("EntityId") .Element("DateTime"); } }
The result of this mapping will result in four (4) tables.
- Table element with one column Id int.
- The Entity_String table with two columns is EntityId: int, String: varchar and the EntityId foreign key for the Id column of the Entity table.
- ... similarly, except for an int column.
- ... similarly, except for a datetime column.
source share