2012-11-07 44 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