gpt4 book ai didi

c# - NHibernate 无法解析属性

转载 作者:太空狗 更新时间:2023-10-29 17:46:30 24 4
gpt4 key购买 nike

我遇到异常 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"));
}

最佳答案

让我更准确地说,并扩展 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

关于c# - NHibernate 无法解析属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com