c# - Conversion failed when converting varchar value xxxx to int using an enum with NHibernate -
i have enum property want store , retrieve database string. nhibernate able store enum string throws following conversion exception upon retrieval:
nhibernate.adoexception: not execute query --> system.data.sqlclient.sqlexception: conversion failed when converting varchar value 'pending' data type int.
based on post mapping enumerations nhibernate jeremy miller created following class:
public class workflowaction { public virtual actionstatus status { get; set; } }
which uses enum:
public enum requeststate { pending, approved, rejected }
which uses class transform string
public class actionstatusenumstring : nhibernate.type.enumstringtype { public actionstatusenumstring() : base(typeof(actionstatus), 50) { } }
and setup property in mapping file this:
<property type="infrastructure.enum.actionstatusenumstring, infrastructure" name="status" column ="status" />
as said works great saving data. however, when want retrieve via parameter receive conversion exception.
return getsession().createquery( @"select distinct requests workflowrequestinformation requests join requests.workflowrequestinformationactionlist actions actions.status = :status .setparameter("status", actionstatus.pending) .list<workflowrequestinformation>();
nhibernate sends actionstatus.pending
database integer. guess because nhibernate running actionstatus.pending.tostring()
.
i can change code either of following , it'll work
// defeats purpose of enum .setparameter("status", "pending") // while feels heavy handed .setparameter("status", enum.getname(typeof(actionstatus), actionstatus.pending))
is there built nhibernate missing makes conversion more automatic?
can explain me why need 'actionstatusenumstring' type? enums should map strings default anyway, need is:
<property name="status" />
i these sort of queries time, mapping enums either strings or ints.
Comments
Post a Comment