Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone 8现有数据库InvalidCastException:指定的强制转换无效_C#_Windows Phone_Sql Server Ce - Fatal编程技术网

C# Windows Phone 8现有数据库InvalidCastException:指定的强制转换无效

C# Windows Phone 8现有数据库InvalidCastException:指定的强制转换无效,c#,windows-phone,sql-server-ce,C#,Windows Phone,Sql Server Ce,我正在使用预填充的数据库。当应用程序启动时,我将数据库从资产复制到isostore。 实体: [Table(Name = "NAMES")] public class NameItem: INotifyPropertyChanged, INotifyPropertyChanging { public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangingEventHandle

我正在使用预填充的数据库。当应用程序启动时,我将数据库从资产复制到isostore。 实体:

[Table(Name = "NAMES")]
public class NameItem: INotifyPropertyChanged,
INotifyPropertyChanging {
    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

    private string _nameString;
    private int _id;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, Name = "_id")]
    public int NameId {
        get {
            return _id;
        }
        set {
            if (value != _id) {
                OnPropertyChanging("NameId");
                _id = value;
                OnPropertyChanged("NameId");
            }
        }
    }

    [Column(IsPrimaryKey = false, Name = "NAME_TEXT")]
    public String NameString {
        get {
            return _nameString;
        }

        set {
            if (_nameString != value) {
                OnPropertyChanging("NameString");
                _nameString = value;
                OnPropertyChanged("NameString");
            }
        }
    }
}
在这里,我尝试从db读取数据:

using(var db = new MyDataContext("isostore:/Database/ff.sdf")) {
    var allNameItems = db.NameItems.ToArray(); // exception on this line
    try {
        MessageBox.Show(allNameItems[0].NameString);
    } catch (Exception ex) {
        MessageBox.Show(ex.Message);
    }
}
有了这段代码,我得到了InvalidCastException,但是db.NameItems.Count起作用。

好的,我读了这段代码,发现了问题,我在db中的id字段是BigInt,在Entity中是int。我把它改为long,它就起作用了