Reputation: 569
This is my first attempt to use NPOCO and I'm trying to read a MS SQLEXPRESS database
NPOCO "2.2.49" from NuGet, SQLEXPRESS 2008 R2, VS 2013, .NET 4.5
Connectionstring is
<connectionStrings>
<add name="TrackTime.Properties.Settings.connectionDB" connectionString="Data Source=ame-PC\SQLEXPRESS;Initial Catalog=TrackTime;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
The code is
db = new Database("TrackTime.Properties.Settings.connectionDB");
var tracks = db.Fetch<trackdatum>();
// List<trackdatum> tracks = db.Fetch<trackdatum>();
trackdatum class
using NPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrackTime
{
[TableName("trackdata")]
[PrimaryKey("trackdata_id")]
[ExplicitColumns]
public partial class trackdatum
{
[Column]
public string trackdata_customer { get; set; }
[Column]
public DateTime trackdata_date { get; set; }
[Column]
public DateTime trackdata_end { get; set; }
[Column]
public int trackdata_id { get; set; }
[Column]
public string trackdata_note { get; set; }
[Column]
public string trackdata_project { get; set; }
[Column]
public DateTime trackdata_start { get; set; }
[Column]
public string trackdata_task { get; set; }
[Column]
public TimeSpan trackdata_worked { get; set; }
}
}
On this line
var tracks = db.Fetch<trackdatum>();
(I have tried the commented line also with the same result.)
I get this exception
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Object must implement IConvertible.
Source=mscorlib
StackTrace:
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at NPoco.PocoData.<>c__DisplayClass19.<GetConverter>b__14(Object src)
at poco_factory_0(IDataReader , trackdatum )
at NPoco.Database.<Query>d__7`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at NPoco.Database.Fetch[T](Sql sql)
at NPoco.Database.Fetch[T](String sql, Object[] args)
at NPoco.Database.Fetch[T]()
SNIP ...
Why? and how do I fix it?
// Anders
Upvotes: 3
Views: 4189
Reputation: 2202
You've probably solved it already but the problem may be that you're fetching a column with datatype TIME
from the database and try to map it to a property with datatype DATETIME
in your C# class. If so, change the datatype to TIMESPAN
in the C# code and it should work I believe.
Upvotes: 5
Reputation: 33381
Depends on your stack trace it needs that your POCO
properties must implement IConvertible
from which the TimeSpan
doesn't.
Upvotes: 4