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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Specifying the .KeyColumn("FooId") fixed the issue. Thank you
No worries - perhaps you might be good enough to flag this as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.