2014-03-12 45 views
6

我有一個Linq DataContext作爲應用程序的數據庫。我已經設置了MVVM模式,並且能夠將新記錄插入到數據庫中。但是,當我加載這些記錄並嘗試更新它們時,記錄的新實例正在後臺創建並且正在使用屬性更改進行更新。所以當他們調用保存命令時,最初加載的記錄實例沒有變化,並且沒有保存。Windows phone 8 MVVM Linq表在NotifyPropertyChanging上創建新實例

從我可以告訴這是事件

  • 負載實例1個
  • 開始更新財產
  • NotifyPropertyChanging叫
  • 新INSTANCE2加載
  • 新實例2被更新的順序
  • 從實例1的UI調用保存更改
  • 次​​
  • 不會進行任何更改,因爲實例1尚未更新

下面是我的代碼有:

/*這是實體*/

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

/*這是數據上下文*/

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

/*這是視圖模型*/

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)); 
     } 
    } 
} 
+0

您沒有顯示將這三部分放在一起的代碼。視圖模型可能會創建一個新的用戶。你如何/在哪裏初始化和保存虛擬機? –

回答

2

在你的MyDataContext我想你會想要下面的,而不是一個基本的單例概念,以便您正在處理同一個對象,從而保存同一個對象的變化。

private static DataContext context = null; 

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

edit-注意,這可能會對您的應用程序產生重大影響。可能需要重新設計一個新的創建時,並且如果/當你應該專門設置爲空。

+0

嗨,感謝您的迴應,這並沒有真正解決我的問題。我確實使用了這個想法,但我也必須在ViewModel本身的datacontext中添加一個成員變量。 看起來好像datacontext的notifyPropertyChanging事件處理程序做了一些錯誤,並嘗試在第一次調用時創建該實體的新實例。 – Koenyn