Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

TypeConverter ignored when creating SqlArgument #415

Open
Chimaine opened this issue Oct 7, 2015 · 4 comments
Open

TypeConverter ignored when creating SqlArgument #415

Chimaine opened this issue Oct 7, 2015 · 4 comments

Comments

@Chimaine
Copy link

Chimaine commented Oct 7, 2015

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.

@Chimaine Chimaine changed the title TypeConverter ignored when using SqlQuery with SqlArgument TypeConverter ignored when creating SqlArgument Oct 7, 2015
@TrevorPilley TrevorPilley added this to the 7.0 milestone Oct 7, 2015
@TrevorPilley
Copy link
Collaborator

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

@Chimaine
Copy link
Author

Chimaine commented Oct 7, 2015

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?

@TrevorPilley
Copy link
Collaborator

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?

@Chimaine
Copy link
Author

Chimaine commented Oct 8, 2015

That will have to wait until Monday when I get back to work, but will do.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants