2014-01-10 103 views
8

我收到異常NHibernate.QueryException無法解析屬性:InsuredId。我是NHibernate的新手,我無法弄清楚。NHibernate無法解析屬性

定義性質

public virtual int InsuredId { get; set; } 
public virtual string Gender { get; set; } 
public virtual DateTime DateOfBirth { get; set; } 
public virtual string SrId { get; set; } 
public virtual string SchoolId { get; set; } 
public virtual string Ssn { get; set; } 
public virtual DateTime GradDate { get; set; } 

將數據映射到屬性

public InsuredMap() 
{ 
    ReadOnly(); 

    Table("Insured"); 
    Id(x => x.Id, "InsuredId"); 
    Map(x => x.Gender, "SexCd"); 
    Map(x => x.DateOfBirth, "BirthDt"); 
    Map(x => x.SrId, "SIDIdNum"); 
    Map(x => x.SchoolId, "SchoolIdTxt"); 
    Map(x => x.Ssn, "SocSecNumTxt"); 
    Map(x => x.GradDate, "GradMthYrNum"); 
} 

功能來獲取所有值

public Entities.Insured GetByInsuredId(int insuredId) 
{ 
    var query = Session.QueryOver<Entities.Insured>() 
     .Where(x => x.InsuredId == insuredId) 
     .Cacheable() 
     .CacheRegion(Constants.EntityCacheRegion); 

    return query.SingleOrDefault(); 
} 

單位測試以測試數據

[Test] 
public void InsuredMapTest() 
{ 
    var insured = repository.GetByInsuredId(714619800); 
    Assert.That(insured.Gender, Is.EqualTo("F")); 
} 
+2

它看起來像你還沒有映射'InsuredId',這就是爲什麼你得到異常 –

回答

10

讓我更精確一點,並延伸Andrew Whitaker的評論。

在映射你說:

Id(x => x.Id, "InsuredId"); 

該信息是:我的實體/類Insured

Id(   // an identificator, the key 
x => x.Id  // represented by the property **Id** (here is the issue) 
, "InsuredId") // its DB representation is the column "InsuredId" 

換句話說,C#屬性

public virtual int InsuredId { get; set; } 

不是映射,與上述狀態換貨,所以它不能被用於查詢

我們可以在查詢中做,做它的工作是

var query = Session.QueryOver<Entities.Insured>() 
    //.Where(x => x.InsuredId == insuredId) 
    .Where(x => x.Id == insuredId) 
    ... 

而且無法解析屬性:InsuredId異常將消失,因爲我們是使用映射的屬性Id

+0

這導致了這個問題,你可以有一個計算字段使用Fluent Nhibernate? – Jess

+0

不完全確定,你在搜索什麼,但可以[這](http://stackoverflow.com/a/13966856/1679310)幫助?我們在NHibernate中有'formula =「''屬性... –

+0

更多類似這樣的:http://stackoverflow.com/questions/2386981/fluent-nhibernate-and-computed-properties – Jess

相關問題