1

I have a simple mapping file:

<class name="Comment">
  <id name="ID" generator="increment" />
  <property name="KnowledgeID" />
</class>

where "KnowledgeID" is a foreign key to another table. I would like this to be indexed for performance reasons. I know that I can create an index in SQL Server Management Studio and linking it through its name:

  <property name="KnowledgeID" index="IX_KnowledgeComment_ID" />

but I was wondering if I can reach the same goal through nHibernate configuration.

So, what's a good standard practice in defining indexes for nHibernate-generated tables?

Emanuele Ciriachi
  • 2,216
  • 2
  • 26
  • 39

1 Answers1

1

Yes you can create indices or unique contraints with nHibernate. Here's some syntax for unique constraints using mapping by code

public AgencyMap()
{
    Property(x => x.Name, m => 
    { 
        m.UniqueKey("UC_AgencyName");
        m.Index("IX_AgencyName");
        m.NotNullable(true); 
    });
    Property(x => x.ORI);

Here's a link to an example using a multiple column index

How do I create an index over multiple fields using NHibernate 3.2 mapping by code?

Community
  • 1
  • 1
Fran
  • 6,440
  • 1
  • 23
  • 35
  • Can this be done only with the Fluent syntax, or there is a way of achieving the same using XML mappings? – Emanuele Ciriachi Jul 23 '16 at 19:01
  • 1
    Yes. http://stackoverflow.com/questions/834565/how-to-create-a-multi-column-index-or-unique-constraint-with-nhibernate – Fran Jul 23 '16 at 19:05