Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to specify the name of the Index property to use for lists in a fluent nhibernate convention?

When mapping a HasMany or HasManyToMany in fluent nhibernate, you can specify the column name to use for the list as a parameter to the AsList() method as follows:

HasMany(c => c.Customers)
    .AsList(c => c.Column("PositionIndex"));

I would prefer to be able to set this using a Fluent NHibernate convention (either a pre-existing one, or a custom one), especially since the default name appears to be "Index" which is a reserved word in MSSQL.

I've tried using a custom convention implementing IHasManyConvention, but the instance parameter does not seem to contain the information about whether its a list, a bag, or a set, and also does not contain the column details for the index column.

public void Apply(IOneToManyCollectionInstance instance)
{

}

Any ideas?

like image 399
Teevus Avatar asked Nov 06 '22 15:11

Teevus


2 Answers

When the convention is being applied, the underlying mapping has already been generated. There is currently no way to change this mapping to an ordered collection (or any other type) through convention.

However, you can still change the type of collection through an IAutoMappingOverride<> as they are applied prior to the conventions.

Even if this is not supported yet, it looks like quite high on the Todo list for the next release. See this thread for further details.

like image 191
nulltoken Avatar answered Dec 23 '22 03:12

nulltoken


just in case someone comes here

since FNH 1.2 it is possible to change bag to list in a convention. With that i implemented:

class CollectionsAreListsConvention : ICollectionConvention
{
    public void Apply(ICollectionInstance instance)
    {
        instance.AsList();
        instance.Key.Column(instance.EntityType.Name + "_id");

        var mapping = (CollectionMapping)instance.GetType()
            .GetField("mapping", BindingFlags.Instance | BindingFlags.NonPublic)
            .GetValue(instance);

        if (!mapping.HasValue(m => m.Index))
        {
            var indexmapping = new IndexMapping();

            indexmapping.AddColumn(new ColumnMapping
            {
                // for Classes with more than one collection to another Class
                Name = instance.Member.Name + "_position",
            });

            mapping.Index = indexmapping;
        }
    }
}

Not perfect but enough for my project with tons of lists

like image 34
Firo Avatar answered Dec 23 '22 02:12

Firo