FluentNHibernate for Dictionary

What is the best way to map a simple dictionary property using Fluent NHibernate?

+3
source share
3 answers

To display the list as a dictionary:

HasMany(x => x.Customers)
  .AsMap();

I have not used it; therefore cannot give an example.

Take a look at the wiki: Cached version of the page , Actual page I gave the cached version of the page since the site does not seem to be working.

+2
source

Using simple class relationships, such as:

public class Foo {
    public virtual IDictionary<string, Bar> Bars { get; set; }
}

public class Bar {
    public virtual string Type { get; set; }
    public virtual int Value { get; set; }
}

You can match this with Fluent NHibernate this way:

mapping.HasMany(x => x.Bars)
       .AsMap(x => x.Type);

Where is Bar.Typeused as a key field in the dictionary.

+6
source
public class PersistedData 
{
    public virtual IDictionary<key, value> Dictionary { get; set; }
}

public class PersistedDataMap : ClassMap<PersistedData>
{
    HasMany(x => x.Dictionary)
            .Table("dict_table")
            .KeyColumn("column_id")
            .AsMap<string>("key")
            .Element("value");
}

This will correctly map Dictionaryto the table dict_tableand use column_idto associate it with the base id.

As a side note, if you want to use Enum as a key in a dictionary, it should be noted that it NHibernate.Type.EnumStringType<MyEnum>can be used instead of a string in .AsMap<string>to use a string value instead of an Ordinal.

+6
source

Source: https://habr.com/ru/post/1717432/


All Articles