0

我正在连接到旧版 sqlserver 数据库。其中一张表的列名为“Primary”。脚本因此而失败。

nhibernate 生成的脚本:SELECT locations0_.CustomerID as CustomerID1_,locations0_.LocationID as LocationID1_,locations0_.LocationID as LocationID2_0_,locations0_.Primary as Primary2_0_,locations0_.CustomerID as CustomerID2_0_ FROM dbo.tblLocation locations0_ WHERE locations0_.CustomerID=?

班级:

public class Location 
{
    public virtual int LocationID { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual int? CustomerID { get; set; }
    public virtual string LocationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string Address3 { get; set; }
    public virtual string City { get; set; }
    public virtual string StateOrProvince { get; set; }
    public virtual string PostalCode { get; set; }
       public virtual datetime? LTimeStamp{ get;set; }
    public virtual bool Primary { get; set; }
}

地图:公共类 TblLocationMap :ClassMap {

    public TblLocationMap() 
    {
        Table("tblLocation");
        //LazyLoad();
        Id(x => x.LocationID).GeneratedBy.Identity().Column("LocationID");
                    References(x => x.Customer).Column("CustomerID");
        Map(x => x.LocationName).Column("LocationName").Length(50);
        Map(x => x.Address1).Column("Address1").Length(200);
        Map(x => x.Address2).Column("Address2").Length(200);
        Map(x => x.Address3).Column("Address3").Length(200);
        Map(x => x.City).Column("City").Length(100);
        Map(x => x.StateOrProvince).Column("StateOrProvince").Length(100);
        Map(x => x.PostalCode).Column("PostalCode").Length(20);
        //Map(x => x.Primary).Column("Primary").Not.Nullable();
        //Map(x => x.LTimestamp).Column("LTimestamp");
        HasMany(x => x.Contacts).KeyColumn("LocationID");
    }

sql:

CREATE TABLE [dbo].[tblLocation] ( [LocationID] [int] IDENTITY(1,1) NOT NULL, [CustomerID] [int] NULL, [LocationName] nvarchar NULL, [Address1] nvarchar NULL, [Address2] nvarchar NULL , [Address3] nvarchar NULL, [City] nvarchar NULL, [StateOrProvince] nvarchar NULL, [PostalCode] nvarchar NULL, [Primary] [bit] NOT NULL, [RecTimestamp] [timestamp] NULL, ( [LocationID] ASC )WITH ( PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GenericADOException:无法初始化集合:[Domain.Locations#466][SQL:SELECT locations0_.CustomerID as CustomerID1_,locations0_.LocationID 作为 LocationID1_,locations0_.LocationID 作为 LocationID2_0_,locations0_.LocationName 作为 Location2_2_0_,locations0_.Address1 作为 Address3_2_0_,locations0_ .Address2 作为 Address4_2_0_,locations0_.Address3 作为 Address5_2_0_,locations0_.City 作为 City2_0_,locations0_.StateOrProvince 作为 StateOrP7_2_0_,locations0_.PostalCode 作为 PostalCode2_0_,locations0_.Primary 作为 Primary2_0_,locations0_.CustomerID 作为 CustomerID2_0_ FROM dbo.tblLocationlocations0_ WHERE locations0_。 ?]

内部异常:{“关键字'Primary'附近的语法不正确。”}

4

1 回答 1

1

我怀疑Primary是一个保留字并且没有正确转义,因此尝试使用反引号隐式转义列名......

例如

Map(x => x.Primary).Column("`Primary`").Not.Nullable();

如果您使用的是 MsSql 服务器,NHibernate 将自动用方括号交换您的反引号[Primary]

奇怪的是模式正在生成方括号,但选择 SQL 不是。

于 2012-11-07T11:20:30.933 回答