You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dapper is throwing an error when running a query in the following scenario:
the parameter is a collection of objects of a given type T
and the type T is not natively supported by the SQL client
and a TypeHandler is defined for this type T
Example:
// a type that is not natively supported by SqlClientpublicstructLocalDate{publicintYear{get;set;}publicintMonth{get;set;}publicintDay{get;set;}}// a type handlerpublicclassLocalDateHandler: SqlMapper.TypeHandler<LocalDate>{privateLocalDateHandler(){/* private constructor */}// Make the field type ITypeHandler to ensure it cannot be used with SqlMapper.AddTypeHandler<T>(TypeHandler<T>)// by mistake.publicstaticreadonly SqlMapper.ITypeHandler Default=new LocalDateHandler();publicoverride LocalDate Parse(object?value){vardate=(DateTime)value!;returnnew LocalDate {Year= date.Year,Month= date.Month,Day= date.Day };}publicoverridevoidSetValue(IDbDataParameterparameter,LocalDatevalue){
parameter.DbType = DbType.DateTime;
parameter.Value =new DateTime(value.Year, value.Month, value.Day);}}// the failing methodpublicvoid RunQueryWithParamCollectionAndCustomTypeHandler(){
SqlMapper.AddTypeHandler(typeof(LocalDate), LocalDateHandler.Default);
var singleParameter =new{ SingleDate =new LocalDate { Year =2014, Month =7, Day =25}};// this succeedsvarresultSingle= connection.Query<int>("SELECT 1 WHERE '2014-07-25' = @SingleDate", singleParameter).Single();varparameters=new{
ListOfDates =newList<LocalDate>{new(){Year=2014,Month=7,Day=25},new(){Year=2014,Month=7,Day=26},}};// this fails with 'No mapping exists from object type LocalDate to a known managed provider native type.'varresult= connection.Query<int>("SELECT 1 WHERE '2014-07-25' IN @ListOfDates", parameters).Single();}
The text was updated successfully, but these errors were encountered:
tlecomte
pushed a commit
to tlecomte/Dapper
that referenced
this issue
Apr 3, 2024
…ameter
FixesDapperLib#2067
The code for `SqlMapper.PackListParameters` was not using the custom type handler. This caused an error like `No mapping exists from object type xxxxx to a known managed provider native type.` when passing a collection parameter to a query, when the type of the collection items is not natively supported by the SQL client, and when a custom `TypeHandler` is registered for it.
Here this is fixed by using the same logic that is already in `DynamicParameters.AddParameter`.
A unit test is added to cover the scenario.
Dapper is throwing an error when running a query in the following scenario:
Example:
The text was updated successfully, but these errors were encountered: