2012-11-07 45 views
0

我连接到传统sqlserver数据库。其中一个表的列名称为“Primary”。由于这个原因,脚本失败了。nhibernate连接到传统数据库

脚本由NHibernate的产生: SELECT locations0_.CustomerID如CustomerID1_,locations0_.LocationID如LocationID1_,locations0_.LocationID如LocationID2_0_,locations0_.Primary如Primary2_0_,locations0_.CustomerID如CustomerID2_0_ FROM dbo.tblLocation locations0_ WHERE locations0_。客户ID =?

类别:

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:类映射 {

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, [地址1] nvarchar的NULL, [地址2] nvarchar的NULL, [地址3] nvarchar的NULL, [市] nvarchar的NULL, [StateOrProvince] nvarchar的NULL, [POSTALCODE] nvarchar的NULL, [主] [比特] NOT NULL, [RecTimestamp] [时间戳] 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]

GO

GenericADOException: 无法初始化集合:[Domain.Locations#466] [SQL:SELECT locations0_.CustomerID为CustomerID1_,locations0_.LocationID为LocationID1_,locations0_.LocationID为LocationID2_0_,locations0_.LocationName为Location2_2_0_,locations0_。地址1作为地址3_2_0_,地址0_地址2作为地址4_2_0_,地址0_地址3作为地址5_2_0_,地址0_。城市作为城市2_0_,地点0_.StateOrProvince作为StateOrP7_2_0_,地点0_.PostalCode作为PostalCode2_0_,地点0_.Primary作为Primary2_0_,地点0_.CustomerID作为CustomerID2_0_从dbo.tblLocation locations0_ WHERE locations0_.CustomerID =?]

内部例外: {“关键字'Primary'附近的语法错误。”}

+0

给我们提供更多信息:你会得到什么错误? – madth3

+0

添加映射,类和实际的数据库表到这个职位,我们可以回答你的问题。 –

+0

已更新。我从表中删除了一些列。当nhibernate使用Primary列创建sql脚本时,它不起作用。但没有它的作品。 – sunny

回答

1

我怀疑Primary是保留字,并没有得到正确地转义,因此尽量含蓄逃逸背部列名蜱....

例如如果您使用的是MSSQL服务器[Primary]

蹊跷的是,架构生成方括号但选择SQL不

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

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

+0

嗨rippo,感谢您的回复。主要列需要在“”而不是“”中。我怎样才能做到这一点?它没有工作的SQL需要:SELECT lo_.CustomerID作为CustomerID1_,lo_.LocationID作为LocationID1_, lo_.LocationID作为LocationID2_0_,lo_.LocationName作为Location2_2_0_,lo_.Address1作为Address3_2_0_, lo_.Address2作为Address4_2_0_,lo_ .Address3为locations0_,lo_.City为City2_0_, lo_.StateOrProvince为StateOrP7_2_0_,lo_.PostalCode为PostalCode2_0_,罗_。 “小学” 作为column9_2_0_, lo_.CustomerID作为CustomerID2_0_ FROM dbo.tblLocation LO_ – sunny

+0

你尝试一个反勾或单引号?有一个区别 – Rippo

+0

我用单引号..它与后面打勾..谢谢堆..你知道什么可以是没有被转换的时间戳的问题?它将数据作为字节数组。 – sunny