这是我第一次尝试使用 NPOCO,我正在尝试读取 MS SQLEXPRESS 数据库
来自 NuGet、SQLEXPRESS 2008 R2、VS 2013、.NET 4.5 的 NPOCO“2.2.49”
连接字符串是
<connectionStrings>
<add name="TrackTime.Properties.Settings.connectionDB" connectionString="Data Source=ame-PC\SQLEXPRESS;Initial Catalog=TrackTime;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
代码是
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; }
}
}
在这条线上
var tracks = db.Fetch<trackdatum>();
(我也尝试了注释行,结果相同。)
我得到了这个例外
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]()
剪辑...
为什么?我该如何解决?
// 安德斯