Windows Phone 8调试带有引用的本地数据库记录重启后不更新

本文关键字:记录 数据库 重启 更新 Phone 调试 引用 Windows | 更新日期: 2023-09-27 18:02:57

我有一个带有本地数据库的Windows Phone 8应用程序,只有当我启动模拟器时,我才用虚拟数据填充数据库。例如,一个实体是Project,该对象有对客户对象

的引用。
private string customerID;
        [Column(CanBeNull = true)]
        public string CustomerID;
        private EntityRef<Customer> _Customer;
        [Association(Storage = "_Customer", ThisKey = "CustomerID")]
        public Customer Customer
        {
            get { return this._Customer.Entity; }
            set 
            {
                NotifyPropertyChanging("Customer");
                this._Customer.Entity = value;
                NotifyPropertyChanged("Customer");
            }
        }  

当在模拟器中通过调试运行应用程序时,此参考的更新工作和新客户被正确保存在数据库中并可以加载。但是,当我停止调试器(不停止模拟器)并再次重新启动调试时,数据库记录将使用上次运行的数据更新,但没有正确的客户引用。因此,当我在一次运行中更改客户时,这是在运行期间保存的,并且从数据库重新加载项目可以正常工作。当我改变项目的名称,这是保存在数据库正确。但是在重新启动之后,只是更改了上一次运行的名称,但没有设置对新客户的引用,而是设置了虚拟数据中的客户。(但这只在启动模拟器时执行。

你能帮我吗?我不知道。

更新:将数据保存到数据库是在ViewModel类中完成的,如下所示:

public void SaveProjectToDB(Project projectToSave)
        {
            int index = -1;
            index = projectCollection.IndexOf(projectToSave);
            if (index > -1)
                projectCollection[index] = projectToSave;
            else
            {
                dellAppDB.Projects.InsertOnSubmit(projectToSave);
                projectCollection.Add(projectToSave);
            }
            dellAppDB.SubmitChanges();
            //dellAppDB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
            projectViewModel  = null;
        }

。对于单次运行,它是有效的。我总是从DataContext类dellAppDB加载新的对象,而不是从ObservableCollections。

Windows Phone 8调试带有引用的本地数据库记录重启后不更新

找到解决方案:不知什么原因,我忘记在关联部分添加"customerID = value.CustomerID;"行。在我的数据处理类中,我只保存了CustomerID,而不是CustomerID。这是不同的,因为只有customerID被保存在数据库中,而不是customerID。

private EntityRef<Customer> _Customer;
        [Association(Storage = "_Customer", ThisKey = "CustomerID", IsForeignKey = true)]
        public Customer Customer
        {
            get { return this._Customer.Entity; }
            set 
            {
                NotifyPropertyChanging("Customer");
                this._Customer.Entity = value;
                if (value != null)
                    customerID = value.CustomerID;
                NotifyPropertyChanged("Customer");
            }
        }