gpt4 book ai didi

c# - 将 SQL 查询映射到 Nhibernate 中的业务对象

转载 作者:行者123 更新时间:2023-11-30 21:15:00 25 4
gpt4 key购买 nike

我想使用 Nhibernate 将 SQL 查询映射到 Business object。 Employee 表中有很多字段,但我只得到三个并且只想映射那些。

这是我的sql查询

<sql-query  name="findEmployeesInfo">
<return alias="emp" class="Calibr.BusinessDocuments.BOs.Employee, BusinessDocuments"/>
<![CDATA[
select (emp.Eid) as {emp.Id},(emp.FirstName) as {emp.FirstName},(emp.LastName) as {emp.LastName} from Employee emp
]]>
</sql-query>

这里是我有构造函数来映射这些列

public Employee(int Id, string FirstName, string LastName)
{
this.Id = Id;
this.FirstName = FirstName;
this.LastName = LastName;
}

DB Employee 表列名: Eid, FirstName, LastName,......

我遇到了这个异常

could not execute query [ select (emp.Eid) as Eid1_0_,(emp.FirstName) as FirstName1_0_,(emp.LastName) as LastName1_0_ from Employee emp

编辑:如果我选择所有列,它将完美运行——Select * from Employee emp

感谢您的帮助。

最佳答案

我认为您会受益于使用 NHibernate 的 ad-hoc mapping特点:

命名查询:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="findEmployeesInfo">
select emp.Eid as Id, emp.FirstName FirstName, emp.LastName as LastName from Employee emp
</sql-query>
</hibernate-mapping>

员工类:

public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
// etc...
}

临时查询:

IList<Employee> employees = session
.GetNamedQuery("findEmployeesInfo")
.SetResultTransformer(Transformers.AliasToBean(typeof(Employee)))
.List<Employee>();

关于c# - 将 SQL 查询映射到 Nhibernate 中的业务对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5964147/

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