TypeConverter ignored when creating SqlArgument
Chimaine opened this issue · comments
According to the wiki page on Type Converters, registering a converter via
TypeConverter.Converters.Add(new MyTypeConverter());
should be enough for converting to and from database values for a specific type. With session.Insert()
it works as expected.
However, when using custom queries with arguments, only DbType mappings are considered.
I'm currently using a little helper to do the conversion for me.
public static SqlArgument GetArgument( object value )
{
var type = value.GetType();
var converter = TypeConverter.For( type );
var result = converter.ConvertToDbValue( value, type );
return new SqlArgument( result );
}
This works fine, however this might be a more common problem then just for me, so something like this could be supported by ML itself.
Do you think that's something that we could just build into the SqlArgument
constructor?
https://github.com/TrevorPilley/MicroLite/blob/develop/MicroLite/SqlArgument.cs#L33
I'm trying to think if that would cause a problem & I don't think it would...
Don't know enough about the workings of session.Insert
and how that would effect that. It could also break code that relies on the current behavior...
Noticed another thing though, session.Fetch<MyType>(sql)
where the SQL is simply SELECT * FROM MyTable
also throws NotSupportedException
unless a DbType mapping is registered in addition to the converter, which I don't think is intended?
OK, well since you have a working solution I'll just leave this to be thought about as part of version 7 instead of trying to update 6.2.
As far as the NotSupportedException
goes, can you raise that as a separate issue with enough of a code sample to repro in an empty console app? (e.g. just the class definition for MyType
, the mapping configuration and the sample call to session.Fetch
?
That will have to wait until Monday when I get back to work, but will do.