johnnyboy
johnnyboy

Reputation: 879

Fluent nHibernate mapping opposite side of References

I've got a table structure similar to the below:

AttributeKey (ID, Title)
AttributeValue (ID, AttributeKeyID, Value)

I've got my mapping all setup so that if I query AttributeValue, it joins to AttributeKey (AttributeValue.AttributeKeyID = AttributeKey.ID) using References, no problem... however, I want to query the AttributeKey table and LEFT JOIN onto the AttributeValue table so that I get all AttributeKeys and any values ascociated to them... how do I map this in fluent nHibernate?

Essentially it's the reverse of a References mapping.

EDIT: I'm aware that the HasMany method is meant to be the reverse of References but using that throws the exception that my property doesn't implement UserCollectionType

Cheers

Upvotes: 0

Views: 787

Answers (1)

Cole W
Cole W

Reputation: 15303

This should be a HasMany in your AttributeKey ClassMap. Your class should look something like this:

public class AttributeKey
{
    public int Id { get; set; }
    public string Title { get; set; }
    public IList<AttributeValue> AttributeValues { get; set; }
}

Your mapping would then look like this:

public class AttributeKeyMap : ClassMap<AttributeKey>
{
    public AttributeKeyMap()
    {
        Id(x => x.Id, "ID");
        Map(x => x.Title);
        HasMany(x => x.AttributeValues)
            .KeyColumn("AttributeKeyID")
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

Upvotes: 1

Related Questions