NHibernate Naming Conventions - Eliminating Keyword Conflicts

How to avoid keyword conflicts when NHibernate generates column names with FluentNHibernate?

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

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

      

+2


source to share


2 answers


I had a similar problem. The fix is ​​to use the FluentNHibernate Column property to place parentheses around the column name for the key property.

Here is the entity with the problematic Key property:

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

      



Here's 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();
        }
    }

      

+1


source


Try the following:

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

      



SQL Server allows you to avoid reserved words by placing square brackets around the text.

0


source







All Articles