John
John

Reputation: 285

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?

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...'

Upvotes: 0

Views: 612

Answers (1)

ovolko
ovolko

Reputation: 2797

From CertificationOfInsuranceEntity definition, it seems like it is an already mapped entity. 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.

The following code should retrieve the correct results:

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

Upvotes: 1

Related Questions