I use .NET with MySql , NPoco
So two constructor : empty and copy.
IConvertible
is implemented.
I successfully got data from another table with .Query
But when I try get data from table Lesson in the line " // ERROR" - I got:
Object must implement IConvertible. - Run Time Error
(so I tried write full SQL query: select* from lesson where ... - same error)
In MySQL FirstDate, LastDate have Date type.
At that, field FromDate don't gives an error message.
I will be grateful for help
Dependencies:
Connection to db:
_connectionString = "Server = 127.0.0.1; Database=test; user id=root; password=root;";
_connection = new MySqlConnection(_connectionString);
_connection.Open();
[TableName("lessons")]
[PrimaryKey("id")]
public class Lesson : IConvertible
{
[Column("id")]
public int? LessonId
{ get;
set; }
[Column("first_date")]
public DateTime FirstDate
{ get;
set; }
[Column("last_date")]
public DateTime LastDate
{ get;
set; } // ERROR here
public Lesson()
{
Console.WriteLine("Constructor Lesson");
}
public Lesson(Lesson torahLesson)
{
LessonId = Lesson.LessonId;
FirstDate = Lesson.FirstDate;
LastDate = Lesson.LastDate;
}
static T ThrowNotSupported<T>()
{
var ex = ThrowNotSupported(typeof(T));
return (T)ex;
}
static object ThrowNotSupported(Type type)
{
throw new InvalidCastException($"Converting type \"{typeof(Lesson)}\" to type \"{type}\" is not supported.");
}
TypeCode IConvertible.GetTypeCode()
{
return TypeCode.Object;
}
.....
DateTime IConvertible.ToDateTime(IFormatProvider provider) => ThrowNotSupported<DateTime>();
.....
}
Full Error Description :
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at NPoco.MappingHelper.<>c__DisplayClass2_0.b__6(Object src)
at NPoco.RowMappers.PropertyMapper.MapValue(GroupResult1 posName, Object[] values, Func
2 converter, Object instance, PocoColumn pocoColumn, Object defaultValue)
at NPoco.RowMappers.PropertyMapper.<>c__DisplayClass8_2.b__3(DbDataReader reader, Object[] values, Object instance)
at NPoco.RowMappers.PropertyMapper.<>c__DisplayClass7_0.b__1(DbDataReader reader, Object[] values, Object instance)
at NPoco.RowMappers.PropertyMapper.Map(DbDataReader dataReader, RowMapperContext context)
at NPoco.MappingFactory.Map(DbDataReader dataReader, Object instance)
at NPoco.Database.Read[T](Object instance, DbDataReader r, PocoData pd)+MoveNext()
at NPoco.Database.QueryImp[T](T instance, Expression1 listExpression, Func
2 idFunc, Sql Sql, PocoData pocoData)+MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at NPoco.Linq.QueryProvider
1.ToList()
at Avidov.Library.Server.Database.DbHelper.Query[T](Expression`1 whereExpression) in E:\MyData\Projects\Comp\LibraryDotNet\Comp.Library\Comp.Library.Serve
\Database\DbHelper.cs:line 169
at Comp.Library.Server.Database.Repositories.LessonRepository.GetNotDeletedLessons(Int32 organizationId, Int32 year) in E:\MyData\Projects\Comp\LibraryDotNet\Comp.Library\Comp.Library.Serve
\Database\Repositories\LessonRepository.cs:line 76
at Comp.Library.Server.LibraryManager.LessonsNumberDaysChanged(Int32 organizationId, DateTime createdAfterDate, Int32 budgetYear) in E:\MyData\Projects\Comp\LibraryDotNet\Comp.Library\Comp.Library.Serve
\LibraryManager.cs:line 81
at Comp.Library.Client.Program.Main(String[] args) in E:\MyData\Projects\Comp\LibraryDotNet\Comp.Library\Comp.Library.Client\Program.cs:line 18" string