NHibernate无法解析属性

 手机用户2602890485 发布于 2023-02-04 15:27

我得到例外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()
        .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"));
}  

Radim Köhler.. 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()
    //.Where(x => x.InsuredId == insuredId)
    .Where(x => x.Id == insuredId)
    ...

并且无法解析属性:InsuredId异常将消失,因为我们正在使用映射属性Id

1 个回答
  • 让我更准确一点,并扩展安德鲁惠特克的评论.

    在您所说的映射中:

    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

    2023-02-04 15:31 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有