简体   繁体   English

C#NHIbernate转换器,用于将ID列映射到Object.ID

[英]C# NHIbernate transformer for ID column mapping to Object.ID

I have the the following c# code to execute a stored procedure using nhibernate but am having an issue transforming the query result to the appropriate object because one of the ID properties on the table is an object on the entity (the column name is 'BusinessId' but the entity property for that is of type 'Business', so it would be 'Business.BusinessId'. How do I properly transform this? 我有以下c#代码使用nhibernate执行存储过程,但是在将查询结果转换为适当的对象时遇到问题,因为表上的ID属性之一是实体上的对象(列名称为'BusinessId'但是其实体属性的类型为“业务”,因此应为“ Business.BusinessId”,如何正确转换呢?

const string sql = "exec dbo.usp_StoredProc";
var query = CurrentSession.CreateSQLQuery(sql)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(CertificationOfInsuranceEntity)));
return query.List<CertificationOfInsuranceEntity>().ToList();

Entity: 实体:

public class CertificationOfInsuranceEntity 
{
    public virtual int CertificationOfInsuranceId { get; set; }
    public virtual BusinessEntity Business { get; set; }
    public virtual string InsuranceType { get; set; }
}

Table: 表:

   CertificationOfInsuranceId int,
   BusinessId int,
   InsuranceType varchar

To clarify, when I execute this, I get an error stating 'Could not find a setter for property 'BusinessId' in class...' 为了澄清,当我执行此操作时,出现错误,提示“在类中找不到属性'BusinessId'的设置器...”

From CertificationOfInsuranceEntity definition, it seems like it is an already mapped entity. 从CertificationOfInsuranceEntity定义看来,它是一个已经映射的实体。 In that case, you should use AddEntity rather than AliasToBeanResultTransformer – the difference is AddEntity is designed to map onto mapped entities while AliasToBeanResultTransformer is (mostly) for plain ad-hoc DTOs http://nhibernate.info/doc/nhibernate-reference/querysql.html . 在那种情况下,您应该使用AddEntity而不是AliasToBeanResultTransformer –区别在于AddEntity旨在映射到映射的实体,而AliasToBeanResultTransformer(主要)用于纯临时DTO http://nhibernate.info/doc/nhibernate-reference/querysql .html

The following code should retrieve the correct results: 以下代码应检索正确的结果:

var query = CurrentSession.CreateSQLQuery(sql)
     .AddEntity(typeof(CertificationOfInsuranceEntity));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM