0

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 :)

Royi Mindel
  • 1,258
  • 12
  • 35
  • have you seen this: http://stackoverflow.com/questions/2254176/how-to-map-idictionarystring-entity-in-fluent-nhibernate – Dai Bok Oct 27 '13 at 13:34
  • just tried it, didn't work, gave me a -not mapped exception for the KeyValuePair class of the dictionary, that solution is for IDictionary – Royi Mindel Oct 27 '13 at 13:56

1 Answers1

0

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);
Firo
  • 30,626
  • 4
  • 55
  • 94