Windows Phone 8 Debug local database records with references doesn't update after restart

StackOverflow https://stackoverflow.com/questions/23624165

  •  21-07-2023
  •  | 
  •  

Question

I have a Windows Phone 8 app with a local database and only when I start the emulator new I fill the database with dummy data. One entity is for example the Project and this object has a reference to the customer object

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");
            }
        }  

When running the app via debugging in the emulator the update of this reference works and the new customer are saved correctly in the database and can be loaded. But when I stop the debugger (not stopping the emulator) and restart the debugging again the database records are updated with the data from the last run but without the correct references for cutomer for example. So when I change the customer in one run this is saved during the run and reloading the project from the database works fine. When I change the name of the project too this is saved in the database correctly. But after the restart just the name is changed from the last run but the reference to the new customer is not set but to the customer from the dummy data. (But this is only executing when starting the emulator.

Can you help. I have no idea.

Update: Saving data to the database is done in a ViewModel class like this:

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;
        }

Nothing more. And for the single run it works. I always load the objects new from the DataContext class dellAppDB and not from the ObservableCollections.

Était-ce utile?

La solution

Found the solution: For no good reason I forgot to add the line "customerID = value.CustomerID;" in the association part. In my data handling classes I just saved the CustomerID and not the customerID. That makes the difference because just the customerID is saved in the database and not the 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");
            }
        } 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top