views:

1221

answers:

3

Is it possible to generate table indexes along with the rest of the database schema with Fluent NHibernate? I would like to be able to generate the complete database DDL via an automated build process.

+8  A: 

Do you mean indexes on columns?

You can do it manually in your ClassMap<...> files by appending .SetAttribute("index", "nameOfMyIndex"), e.g. like so:

Map(c => c.FirstName).SetAttribute("index", "idx__firstname");

or you can do it by using the attribute features of the automapper - e.g. like so:

After having created your persistence model:

{
    var model = new AutoPersistenceModel
    {
        (...)
    }

    model.Conventions.ForAttribute<IndexedAttribute>(ApplyIndex);
}


void ApplyIndex(IndexedAttribute attr, IProperty info)
{
    info.SetAttribute("index", "idx__" + info.Property.Name");
}

and then do this to your entities:

[Indexed]
public virtual string FirstName { get; set; }

I like the latter. Is is a good compromise between not being non-instrusive to your domain model, yet still being very effective and clear on what is happening.

mookid8000
That's exactly what I was looking for. Thank you.
Todd Brooks
+6  A: 

Mookid's answer is great and helped me a lot, but meanwhile the ever evolving Fluent NHibernate API has changed.

So, the right way to write mookid sample now is the following:

//...
model.ConventionDiscovery.Setup(s =>
            {
                s.Add<IndexedPropertyConvention>();
                //other conventions to add...
            });

where IndexedPropertyConvention is the following:

public class IndexedPropertyConvention : AttributePropertyConvention<IndexedAttribute>  
{
    protected override void Apply(IndexedAttribute attribute, IProperty target)
    {
         target.SetAttribute("index", "idx__" + target.Property.Name);
    }
}

The [Indexed] attribute works the same way now.

Yann Schwartz
+8  A: 

In more recent versions of Fluent NHibernate, you can call the Index() method to do this rather than using SetAttribute (which no longer exists):

Map(x => x.Prop1).Index("idx__Prop1");

Paul Batum