我遇到异常NHibernate.QueryException
:无法解析属性:InsuredId 。我是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"));
}
答案 0 :(得分:11)
让我更加准确,并延伸安德鲁·惠特克的评论。
在您所说的映射中:
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