I am getting the nebulous
input string was not in a correct format
and I can see it was asked before, but I have attempted to get the type of the variable in order but to no avail. I would appreciate some assistance.
Here are my log messages:
WARN 07:05:02 The LINQ expression 'where (((Convert([e].p_Expiry_ID, Int32) == Convert(__tmpExpiryID_0, Int32)) AndAlso ([e].p_Valid_From <= DateTime.Now))
AndAlso ((ToDateTime([e].p_strValid_To) >= DateTime.Now) OrElse ([e].p_strValid_To == null)))' could not be translated and will be evaluated locally.
The Int32 is interesting here, because my definitions are Int16, then comes the exception:
SELECT `e`.`Expiry_ID`, `e`.`Valid_From`, `e`.`Day`, `e`.`Description`, `e`.`Expiry_Type`, `e`.`Export`, `e`.`Fixed_Interval_ID`, `e`.`Number_Interval_Units`, `e`.`Unit_ID`, `e`.`Valid_To`
FROM `ExpiryMaster` AS `e`
ORDER BY `e`.`Valid_From`
ERROR 07:05:02 An exception occurred in the database while iterating the results of a query for context type 'VT_OnlineCore.Models.ApplicationDbContext'.
System.InvalidOperationException: An exception occurred while reading a database value. See the inner exception for more information. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at MySql.Data.MySqlClient.MySqlDataReader.GetInt32(Int32 i)
at lambda_method(Closure , DbDataReader )
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
at lambda_method(Closure , DbDataReader )
at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass17_1`1.<CompileQueryCore>b__0(QueryContext qc)
Now, here is the code that makes the call. Because the parameter variable is nullable I have tried moving it to a fixed type beforehand:
CExpiryMasterBase ExpiryRecord = new CExpiryMasterBase();
Int16 tmpExpiryID = (Int16)workingTicket.ExpiryID;
ExpiryRecord = repositoryExpiryMaster.ExpiryMaster
.Where(e => e.p_Expiry_ID == tmpExpiryID
&& e.p_Valid_From <= DateTime.Now
&& ((Convert.ToDateTime(e.p_strValid_To) >= DateTime.Now) || (e.p_strValid_To == null)))
.OrderBy(e => e.p_Valid_From)
.FirstOrDefault();
This is the property used:
public Int16 p_Export
{
get
{
return m_Export;
}
set
{
m_Export = value;
}
}
private Int16 m_Expiry_ID;
This is the field definition:
Expiry_ID tinyint(2) NO PRI
The database is MySQL
What I have tried:
Removing Valid From and Valid To from the query, using a temp field to avoid having a nullable field in the query.
I have also tried an elementary direct SQL query and got the same error result:
IQueryable<CExpiryMasterBase> data = context.ExpiryMaster
.FromSql(@"SELECT * FROM ExpiryMaster
WHERE Expiry_ID = 1
");