Windows Phone 8 MVVM Linq table Creates a New Instance in NotifyPropertyChanging

I have a Linq DataContext as the database for the application. I created an MVVM template and can insert new records into the database. However, when I download these records and try to update them, a new instance of the record is created in the background and updated with property changes. Therefore, when the user interface invokes a save command, the originally loaded instance of the record has no changes and is not saved.

from what i can say is a sequence of events

  • Download instance 1
  • Start property update
  • NotifyPropertyChanging is called
  • New instance uploaded2
  • Updated new instance
  • Call save changes from user interface for instance 1
  • No changes since instance 1 is not updated

Below is the code:

/ * This is the Essence * /

[Table] public class User : IDisposable, INotifyPropertyChanged, INotifyPropertyChanging { private MyDataContext context; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangingEventHandler PropertyChanging; private void NotifyPropertyChanging(String propertyName) { PropertyChangingEventHandler handler = PropertyChanging; if (null != handler) { handler(this, new PropertyChangingEventArgs(propertyName)); } } public void Dispose() { context.Dispose(); } private Guid _id; [Column(IsPrimaryKey = true, IsDbGenerated = false, DbType = "UNIQUEIDENTIFIER NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert)] public Guid Id { get { return _id; } set { if (_id != value) { NotifyPropertyChanging("Id"); _id = value; NotifyPropertyChanged("Id"); } } } private string _name; [Column(CanBeNull = false)] public string Name { get { return _name; } set { if (_name != value) { NotifyPropertyChanging("Name"); // This line creates the new entity _name = value; NotifyPropertyChanged("Name"); } } } public User() { this.context = MyDataContext.GetContext(); } public override void SaveChanges() { if (_id == Guid.Empty) { this.Id = Guid.NewGuid(); context.Users.InsertOnSubmit(this); context.SubmitChanges(); } else { context.SubmitChanges(); } } public static User NewInstance() { return new User { Name = String.Empty }; } } 

/ * This is the data context * /

 public class MyDataContext : DataContext { // Specify the connection string as a static, used in main page and app.xaml. public static string ConnectionString = "Data Source=isostore:/MyApp.sdf;Password=pwd"; public MyDataContext(string connectionString) : base(connectionString) { } public static MyDataContext GetContext() { var context = new MyDataContext(ConnectionString); return context; } public Table<User> Users; } 

/ * This is a view model * /

 public sealed class UserViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } private User _user; public UserViewModel(Guid id) { using (MyDataContext context = new MyDataContext(MyDataContext.ConnectionString)) { _User = context.User.First(u => u.Id == id); } } public UserViewModel(User user) { _user = user; } public UserViewModel() { _user = User.NewInstance(); } public string Name { get { return _user.Name; } set { _user.Name = value; NotifyPropertyChanged("Name"); } } private ICommand _saveCommand; public ICommand SaveCommand { get { return _saveCommand ?? (_saveCommand = new GenericCommand(() => { _user.SaveChanges(); }, true)); } } } 
+6
source share
1 answer

In your MyDataContext I would think that you want to use the basic singleton concept below so that you work on the same object and thereby save the same object with the changes.

 private static DataContext context = null; public static MyDataContext GetContext() { if(context == null) context = new MyDataContext(ConnectionString); return context; } 

edit- Note. This can significantly affect your application in the big picture. A redesign may be required when a new one is created, and if / when you must set it to null.

+2
source

All Articles