I have been looking all around for the correct mapping, with no success.
Got to the point i started finding people who gave up https://groups.google.com/forum/#!topic/fluent-nhibernate/u_MYdOTD1Jk
So - how do you map an
IDictionary<Entity1,Entity2> ?
I would post what i tried, but i'm at my 15~th try now..
public class Entity1Map : ClassMap<Entity1>
{
public Entity1Map()
{
Id(x=> x.ID);
}
}
public class Entity2Map : ClassMap<Entity2>
{
public Entity2Map()
{
Id(x=> x.ID);
}
}
public class Entity3 { public IDictionary<Entity1,Entity2> Dict { get;set; } }
public class Entity3Map : ClassMap<Entity3>
{
public Entity3Map()
{
Id(x=> x.ID);
//DictMap??
}
}
Thanks a lot :)
Use EntityMap. The following is working for me
public class Entity3Map : ClassMap<Entity3>
{
public Entity3Map()
{
Id(x => x.Id);
HasManyToMany(x => x.Dict)
.Table("linkTable")
.Cascade.All()
.AsEntityMap();
}
}
var e1 = new Entity1();
var e2 = new Entity2();
using (var tx = session.BeginTransaction())
{
session.Save(e1);
session.Save(new Entity3 { Dict = { { e1, e2 } } });
tx.Commit();
}
session.Clear();
var entity3 = session.Query<Entity3>().FetchMany(x => x.Dict).ToList().First();
Assert.Equal(1, entity3.Dict.Count);
Assert.Equal(e1.Id, entity3.Dict.First().Key.Id);
Assert.Equal(e2.Id, entity3.Dict.First().Value.Id);