2
votes

How can I map these Entities using mapping-by-code:

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

public class Bar
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

I found this thread, but it does not map an entity, only simple types. I tried many mappings, including automapping:

Map(x => x.Bars,
    m =>
    {
        m.Key(k => k.NotNullable(true));
        m.Cascade(Cascade.All);
    },

But most of them throw these two errors:

  1. Foreign key (Bars [idx])) must have same number of columns as the referenced primary key (Bars [FooId, idx]).
  2. An association from the table FoosToStrings refers to an unmapped class: System.String.

Any help will be highly appreciated. Thanks. :)

1
This translates to the index-many-to-any mapping which I believe has some issues in FluentNHibernate. Please see this related question with workaround. stackoverflow.com/questions/8542298/…. - Phil Degenhardt
I am not using Fluent. I am using mapping by code. - Yogesh
Apologies. Need to read the question more closely. - Phil Degenhardt
No need for apologies. Happens. :) - Yogesh

1 Answers

0
votes

i think this should work

Map(x => x.Bars,
    entryMap => entryMap.Key(k => k.Column("foo_id")),
    keymap => keymap.ManyToMany(m => m.Column("bar_Id")),
    elementMap => elementMap.Element(m => m.Column("value")));