How do I map a dictionary using Fluent NHibernate automapping?

大憨熊 提交于 2019-12-06 13:39:19

It is impossible with NHibernate.

Arnis Lapsa

Found some traces that this isn't possible. Some traces, that it's recently implemented.

Still investigating. :)


This looks quite promising (didn't test yet).

So, in your case it should look like=>

public class LandMap : ClassMap<Land>
{
    public LandMap()
    {
        (...)

        HasMany(x => x.Damages)
            .WithTableName("Damages")
            .KeyColumnNames.Add("LandId")
            .Cascade.All()
            .AsMap<string>(
                index => index.WithColumn("DamageType").WithType<string>(),
                element => element.WithColumn("Amount").WithType<int>()
            );
    }
}

Keep in mind - it should. I didn't test it.

A possible workaround that should in theory work with automapping:

public class DamagesDictionary : Dictionary<string, int>
{
}

Land.cs

public class Land
{
   public virtual DamagesDictionary Damages { get; set; }
   // and other properties
}

or a more generic approach...

public class StringKeyedDictionary<T> : Dictionary<string, T>
{
}

Land.cs

public class Land
{
   public virtual StringKeyedDictionary<int> Damages { get; set; }
   // and other properties
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!