WP7,本地数据库,在日期时间列上强制转换异常

本文关键字:转换 异常 日期 数据库 WP7 时间 | 更新日期: 2023-09-27 18:34:05

我正在尝试将带有日期戳的数据存储在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。

是我看错了地方还是我错过了什么?

WP7,本地数据库,在日期时间列上强制转换异常

我也有类似的问题。

就我而言,它缺少表的"服务器数据类型"属性上的值。我把属性"Type"定义为日期时间,但这还不够。