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
I have found a bug related to the where condition, what is not matching due to wrong DbType of the DbParameter. Luckily, I am able to reproduce it in a kind of unit test using sqlite.
In this example, count (other methods could be affected as well) is not returning the right value. The condition is simple, but the types are a bit different. Database is using string/text, C# is using DateTime. Therefore, I use a PropertyHandlerMapper.
I debugged down the whole path and found that on DbCommandExtensions.cs:63 the DbType of the DbParameter changed to DateTime. I would assume this is wrong due to the PropertyHandlerMapper. The PropertyHandler is called a view lines later (DbCommandExtensions.cs:312-315) the dbtype is not set.
var parameter = command.CreateParameter();
// Set the values
parameter.ParameterName = name.AsParameter(DbSettingMapper.Get(command.Connection));
parameter.Value = value ?? DBNull.Value; // <-- this line is chaging DbType
DbCommandExtensions.cs:63
Example:
usingSystem;usingSystem.Data.SQLite;usingSystem.Globalization;usingRepoDb;usingRepoDb.Enumerations;usingRepoDb.Interfaces;usingRepoDb.Options;usingXunit;namespacetest;publicclassStringToDateTimePropertyHandler:IPropertyHandler<string,DateTime>{publicDateTimeGet(stringinput,PropertyHandlerGetOptionsproperty){varoutput=DateTime.Parse(input,null,DateTimeStyles.RoundtripKind);returnoutput;}publicstringSet(DateTimeinput,PropertyHandlerSetOptionsproperty){returninput.ToUniversalTime().ToString("o");}}publicclassDateDemo{publicGuidId{get;set;}publicDateTimeDate{get;set;}}publicclassDemo{[Fact]publicvoidTest(){GlobalConfiguration.Setup().UseSQLite();PropertyHandlerMapper.Add<DateTime,StringToDateTimePropertyHandler>(newStringToDateTimePropertyHandler(),true);varpath="C:\\temp\\test.db";varconnectionString=$"Data Source={path}; Mode=Memory; Cache=Shared";varconnection=newSQLiteConnection(connectionString);connection.ExecuteNonQuery(@$"CREATE TABLE IF NOT EXISTS [{nameof(DateDemo)}] ({nameof(DateDemo.Id)} TEXT PRIMARY KEY,{nameof(DateDemo.Date)} TEXT );");connection.Insert(newDateDemo{Id=Guid.NewGuid(),Date=DateTime.MaxValue});varresult=connection.Count<DateDemo>(where:newQueryField("Date",Operation.Equal,DateTime.MaxValue));Assert.Equal(1,result);}}
The text was updated successfully, but these errors were encountered:
Bug Description
I have found a bug related to the where condition, what is not matching due to wrong DbType of the DbParameter. Luckily, I am able to reproduce it in a kind of unit test using sqlite.
In this example, count (other methods could be affected as well) is not returning the right value. The condition is simple, but the types are a bit different. Database is using string/text, C# is using DateTime. Therefore, I use a PropertyHandlerMapper.
I debugged down the whole path and found that on DbCommandExtensions.cs:63 the DbType of the DbParameter changed to DateTime. I would assume this is wrong due to the PropertyHandlerMapper. The PropertyHandler is called a view lines later (DbCommandExtensions.cs:312-315) the dbtype is not set.
DbCommandExtensions.cs:63
Example:
The text was updated successfully, but these errors were encountered: