我在 Windows Phone 8 应用程序中遇到本地数据库问题。
这是我的 DatabaseManager 和模型
public class DatabaseManager : DataContext
{
// Specify the connection string as a static, used in main page and app.xaml.
public static string DBConnectionString = "Data Source=isostore:/LocalMainDatabase.sdf";
// Pass the connection string to the base class.
public DatabaseManager(string connectionString) : base(connectionString) { }
// Specify a single table for the to-do items.
//public Table<GroupDatabaseModel> GroupDbModel;
public Table<GroupDatabaseModel> GroupDbModel;
}
[Table]
public class GroupDatabaseModel : INotifyPropertyChanged, INotifyPropertyChanging
{
private int id;
//private string name { get; set; }
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id
{
get
{
return id;
}
set
{
if (id != value)
{
NotifyPropertyChanging("Id");
id = value;
NotifyPropertyChanged("Id");
}
}
}
/*[Column(IsPrimaryKey = false, IsDbGenerated = true, DbType = "NVarChar(30) NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
NotifyPropertyChanging("Name");
name = value;
NotifyPropertyChanged("Name");
}
}
}*/
public event PropertyChangingEventHandler PropertyChanging;
public void NotifyPropertyChanging(string propertyName)
{
if (this.PropertyChanging != null)
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
在我的 ViewModel 我创建数据库
DatabaseManager db;
using (db = new DatabaseManager("isostore:/LocalMainDatabase.sdf"))
{
if (db.DatabaseExists() == false)
{
// Create the database.
db.CreateDatabase();
GroupDatabaseModel k1 = new GroupDatabaseModel { Id = 11 };
db.GroupDbModel.InsertOnSubmit(k1);
try
{
db.SubmitChanges();
}
catch (Exception exx)
{
// Console.WriteLine(exx);
// Make some adjustments.
// ...
// Try again.
db.SubmitChanges();
}
// Define query to gather all of the to-do items.
var toDoItemsInDB = from GroupDatabaseModel todo in db.GroupDbModel select todo;
不幸的是,我在提交更改时遇到异常:
“System.Data.Linq.ni.dll 中发生了‘System.NotSupportedException’类型的异常,但未在用户代码中处理
附加信息:此数据提供程序不支持插入仅由数据库生成的值组成的行。”
怎么了 ?