Daniel A. White
Daniel A. White

Reputation: 191058

NHibernate Naming Conventions - Eliminate Keyword Conflicts

How do I escape keyword conflicts when NHibernate generates my column names using FluentNHibernate?

NHibernate generated this for me and "Key" is a conflict.

create table Setting (
       Key NVARCHAR(MAX) not null,
       Value NVARCHAR(MAX) null,
       primary key (Key)
)

Upvotes: 2

Views: 735

Answers (2)

Sean
Sean

Reputation: 1228

I had a similar problem. The fix is to use FluentNHibernate's Column property part to put brackets around the column name for the key property.

Here is an entity with a problematic Key property:

public class MyEntity
    {
        public virtual int Id { get; set; }
        public virtual string Key { get; set; }
    }

Here is how to specify the column name in the mapping:

public class MyEntityMap : ClassMap<MyEntity>
    {
        public MyEntityMap()
        {
            Id(x => x.Id).GeneratedBy.Assigned();

            // Note the brackets around Key
            Map(x => x.Key).Column("[Key]").Not.Nullable();
        }
    }

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351758

Try this:

create table Setting (
       [Key] NVARCHAR(MAX) not null,
       Value NVARCHAR(MAX) null,
       primary key (Key)
)

SQL Server allows you to escape reserved words by putting square brackets around the text.

Upvotes: -1

Related Questions