nhibernate连接到旧数据库

时间:2012-11-07 01:41:59

标签: nhibernate fluent-nhibernate

我正在连接旧的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_ 来自dbo.tblLocation locations0_ locations Where__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,     [主要] [位]非空,     [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] )[主要] TEXTIMAGE_ON [主要]

GO

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

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

1 个答案:

答案 0 :(得分:1)

我怀疑Primary是一个保留字,并且没有正确转义,因此尝试使用后退标记隐式地转义列名....

e.g。

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

如果你使用MsSql服务器[Primary]

,NHibernate会自动用方括号交换你的反引号

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