我正在使用预先填充的数据库。当应用程序启动时,我将db从资产复制到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()可以工作。