0

How do I map Dictionaries with FluentNHibernate?

IDictionary<SomeEnum, bool>

and..

IDictionary<string, bool>

?

like:

public class SomeClass
{    
   public int Id {get;set;}
   public IDictionary<SomeEnum, bool> Dictionary2 {get;set;} 
   public IDictionary<string, bool> Dictionary1 {get;set;} 
}
bretddog
  • 5,411
  • 11
  • 63
  • 111

2 Answers2

1

It seems the mapping for Dictionary can be done as follows:

// IDictionary<string, bool>
HasMany(x => x.Dictionary1).AsMap<string>("keyColumn").Element("Enabled");

// IDictionary<SomeEnum, bool>  (Enum will be mapped as int)
HasMany(x => x.Dictionary2).AsMap("SomeEnum").Element("Enabled");  

// IDictionary<Entity, string>
HasMany(x => x.Dictionary3).AsEntityMap().Element("valueColumn"); 
bretddog
  • 5,411
  • 11
  • 63
  • 111
0

As far as I know, you can't map dictionary to the database. Moreover, there is no equivalent for enum in SQL Server. To implement IDictionary<string, bool>, I guess, you need to create separate table, that will contain Id, your String, Boolean values and FK to SomeClass (many-to-one relationship). With enum dictionary is the same case, but the contents of that foreign table depends on your needs.

Eadel
  • 3,797
  • 6
  • 38
  • 43