2014-01-10 185 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

相关问题