我正在尝试将带有 DateStamp 的数据存储在 WP7 的本地数据库中。我遵循了MSDN 指南并创建了以下代码:
[Table]
public class HistoricGame
{
[Column(IsDbGenerated = true, IsPrimaryKey = true)]
public int Id { get; set; }
[Column]
public DateTime DateStamp { get; set; }
[Column]
public string GameId { get; set; }
[Column]
public int Score { get; set; }
[Column]
public int LongestSequence { get; set; }
}
public class HistoricGameContext : DataContext
{
public const string ConnectionString = "Data Source=isostore:/NumbersNerdDB.sdf";
public HistoricGameContext()
:base(ConnectionString)
{}
public Table<HistoricGame> HistoricGames
{
get { return GetTable<HistoricGame>(); }
}
}
这被称为写入数据库:
private void StoreThisGame()
{
using (var db = new HistoricGameContext())
{
if (!db.DatabaseExists())
db.CreateDatabase();
var game = new HistoricGame
{
DateStamp = DateTime.Now,
GameId = "1",
LongestSequence = _currentGame.LongestCorrectSequence,
Score = _currentGame.TotalPoints
};
db.HistoricGames.InsertOnSubmit(game);
db.SubmitChanges(); //<- This is where it throws an InvalidCastException
}
}
但是 db.SubmitChanges();
不断抛出InvalidCastException
消息“无法从类型'System.DateTime'转换为类型'System.Byte []'。”
我找不到任何指向数据库相关问题的内容,虽然我使用的原始示例不包含 DateTime,但Silverlightshow.net上的示例确实在非常相似的上下文中使用了 DateTime。
我是在看错地方还是遗漏了什么?