Kristian Kristensen’s Blog


Creating a Multi Column Index using Fluent NHibernate

Posted in Code,FluentNHibernate by Kristian Kristensen on the March 20th, 2011

Using Fluent NHibernate I wanted to specify my indexes in my auto mapping code. That way I’d have them when using Schema Update, etc. from Fluent NHibernate/NHibernate. Doing that is a matter of using the Index method on your mapping objects, and correlating the columns you want to include in the index using the index name.

 var model = new AutoPersistenceModel();
 model.AddEntityAssembly(Assembly.GetExecutingAssembly())
    .Where(t => t.Namespace.EndsWith("Entities"));

model.Override<MyPoco>(map =>
{
 map.Map(x => x.ColumnOne).Index("IX_MyColumns");
 map.Map(x => x.ColumnTwo).Index("IX_MyColumns");
});

When generating the schema it’ll include the SQL for creating an index named “IX_MyColumns” over ColumnOne and ColumnTwo.

  • If you like my writing you should subscribe to my RSS feed.

    Using the same mapping for Firebird and Sql Server from Fluent NHibernate

    Posted in Code,FluentNHibernate by Kristian Kristensen on the March 4th, 2011

    In a recent project I’ve been using Fluent NHibernate to generate my NHibernate mappings automatically from my POCO’s. Another requirement in this project is that when deployed the database server should run Firebird. I’ve been using Microsoft Sql Server locally, and really wanted to be able to switch seamlessly between the two databases. Primarily if it turned out later that Sql Server would be the way to go instead of Firebird.
    When switching to Firebird my mappings didn’t work. The reason is that Firebird uses generators for auto-incrementing columns. Using auto-mapping and an auto-incrementing column that’ll work between Firebird and Sql Server is a matter of writing this in your auto mapping overrides:

    model.Override<MyPoco>(map =>  {
      map.Id(x => x.Id).GeneratedBy.Native("MyPoco_Gen");
    }
    

    When the schema is generated from Fluent NHibernate it’ll work with SQL Server and Firebird.

    This post on Stackoverflow describes how to do it with XML and regular NHiberante mappings.

  • If you like my writing you should subscribe to my RSS feed.