0

I have an entity:

public class Foo
{
    public virtual int Id;
    public virtual IEnumerable<string> Bars;
}

And its mapping:

public class FooMapping : ClassMap<Foo>
{
    public FooMapping()
    {
        Table("foo_table_in_database");
        Id(x => x.Id, "Id");
        HasMany(x => x.Bars)
                .AsList()
                .Table("bars_table_in_db")
                .Element("BarId", m =>
                {                    
                    m.Type<string>();
                });
    }
}

And an exception returned inside the entity insteaf of the expected result :(

base = {"could not initialize a collection: [Loya.Services.CouponsWeb.Promotion.LoyCouponCustomerGroups#2][SQL: SELECT loycouponc0_.Promotion_id as Promotion3_0_, loycouponc0_.LoyCustomerGroupId as LoyCusto1_0_, loycouponc0_.Index as Index0_ FROM loy_promotion__cu...

Database tables :

foo_table : *Id, other properties

bar_table : *FooId, *BarId

My aim is to get a List of BarId's (strings) in my Foo. How do I map it properly?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
evictednoise
  • 587
  • 1
  • 7
  • 19

1 Answers1

1

I think you might need to specify the KeyColumn. I do something similar in one of my solutions and I would map the entities above as follows...

        mapping.HasMany(x => x.Bars)
            .Schema("Schema")
            .Table("FooBars")
            .Element("Bar")
            .KeyColumn("FooID")
            .ForeignKeyConstraintName("FK_FooBar_Foo")
            .Not.Inverse()
            .Cascade.All()
            .Fetch.Select();

This will give a table called Schema.FooBars. It will have a FooID column (which is a foreign key back to the Foo table) and a Bar column which contains the value in your Bars collection.

amcdermott
  • 1,565
  • 15
  • 23