This question is locked. New answers and comments are not allowed.
I have an enum on my entity. In the database, it is represented by a char. Naturally, I made an AdoTypeConverter that maps the chars to the enum. For reading, this seems to work just fine. Writing I haven't had a chance to test yet.
But when I query, i get an exception saying "Conversion failed when converting the varchar value 'G' to data type int", for example.
Since all I have are blog posts and github code samples to go off of instead of actual documentation, I'm at a loss to why querying doesn't work.
So, to set up a full example:
Below, EnumDbValueConverter is a class that maps enums values to database values (string).
A side note: CreateLiteralSql is never called.
public
enum
CompanyType
{
General,
LawFirm
}
public
class
Company
{
public
int
PrimaryKey {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
CompanyType Type {
get
;
set
; }
}
public
class
CompanyTypeConverter : AdoTypeConverter
{
public
override
Type DefaultType
{
get
{
return
typeof
(CompanyType); }
}
public
override
object
Read(
ref
DataHolder holder)
{
bool
isNull = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = isNull;
if
(isNull)
{
holder.ObjectValue = EnumDbValueConverter.ConvertDbValue<CompanyType>(
null
);
}
else
{
holder.ObjectValue = EnumDbValueConverter.ConvertDbValue<CompanyType>(holder.Reader.GetValue(holder.Position).ToString());
}
return
(holder.Box) ? holder.ObjectValue :
null
;
}
public
override
void
Write(
ref
DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if
(!holder.NoValue)
{
string
s = EnumDbValueConverter.ConvertEnumValue((CompanyType)holder.ObjectValue);
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
public
override
bool
CreateLiteralSql(
ref
DataHolder holder)
{
if
(holder.NoValue)
{
holder.StringValue =
this
.NullValueSql;
return
false
;
}
else
{
holder.StringValue = EnumDbValueConverter.ConvertEnumValue((CompanyType)holder.ObjectValue);
return
true
;
}
}
}
// this fails with the message "Telerik.OpenAccess.RT.sql.SQLException: Conversion failed when converting the varchar value 'G' to data type int."
return
Repository.GetCompanies().Where(c => c.Type == CompanyType.LawFirm);