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 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)); } } }
source share