2012-06-17 80 views
0

我是新手Windows Phone 7开发人员,我遇到了Linq to SQL问题。我试图更新一个对象,但它不起作用。我得到想要更新的对象并修改了我想要的属性,但是当我调用SaveChanges时,它的数据没有在数据库上更新。Linq-to-SQL不在WP7上更新

我已经使用ISETool下载了数据库,并且检查数据没有更新。

奇怪的是查询和插入方法工作正常,但我不知道为什么更新它不是。

这里的实体和更新方法的代码:

[Table] 
public class Entrada : INotifyPropertyChanged, INotifyPropertyChanging 
{ 
    private int _Id; 
    [Column] 
    private int _DiaId; 
    [Column] 
    private int _ProjetoId; 

    private EntityRef<Dia> _Dia; 
    private EntityRef<Projeto> _Projeto; 

    private DateTime _Chegada;   
    private DateTime? _Saida; 

    [Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, DbType = "INT NOT NULL Identity")] 
    public int Id 
    { 
     get 
     { 
      return _Id; 
     } 
     set 
     { 
      if (value != _Id) 
      { 
       NotifyPropertyChanging("Id"); 
       _Id = value; 
       NotifyPropertyChanged("Id"); 
      } 
     } 
    } 

    [Column(CanBeNull=false)] 
    public DateTime Chegada 
    { 
     get 
     { 
      return _Chegada; 
     } 
     set 
     { 
      if (value != _Chegada) 
      { 
       _Chegada = value; 
       NotifyPropertyChanged("Chegada"); 
      }      
     } 
    } 

    [Association(Storage = "_Dia", ThisKey = "_DiaId", OtherKey="Id", IsForeignKey=true)] 
    public Dia Dia 
    { 
     get { return _Dia.Entity; } 
     set 
     { 
      NotifyPropertyChanging("Dia"); 
      _Dia.Entity = value; 

      if (value != null) 
      { 
       _DiaId= value.Id; 
      } 

      NotifyPropertyChanging("Dia"); 
     } 
    } 

    [Column(CanBeNull=true)] 
    public DateTime? Saida 
    { 
     get 
     { 
      return _Saida; 
     } 
     set 
     { 
      if (value != _Saida) 
      { 
       _Saida = value; 
       NotifyPropertyChanged("Saida"); 
      }      
     } 
    } 

    [Association(Storage = "_Projeto", ThisKey = "_ProjetoId", OtherKey = "Id", IsForeignKey = true)] 
    public Projeto Projeto 
    { 
     get 
     { 
      return _Projeto.Entity; 
     } 
     set 
     { 
      NotifyPropertyChanging("Projeto"); 
      _Projeto.Entity = value; 

      if (value != null) 
      { 
       _ProjetoId = value.Id; 
      } 

      NotifyPropertyChanging("Projeto"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public event PropertyChangingEventHandler PropertyChanging; 

    private void NotifyPropertyChanged(String propertyName) 
    { 
     if (null != PropertyChanged) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private void NotifyPropertyChanging(string propertyName) 
    { 
     if (PropertyChanging != null) 
     { 
      PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
     } 
    } 
} 

//UPDATE CODE: 
var query = (from e in timeSheetDB.Entradas where e.Dia.Id == this.Dia.Id && (!e.Saida.HasValue) select e); 
var entrada = query.FirstOrDefault(); 
if (entrada != null) 
{ 
    entrada.Saida = DateTime.Now; 
} 
timeSheetDB.SubmitChanges(); 

我还检查GetChangeSet()Updates.Count(),但它总是0。我希望你能帮助我:-) 谢谢你们!

回答

0

它似乎是你没有提高事件的Saida财产;这个事件对于LINQ到SQL来说很重要,所以我建议更新代表列的所有成员来提高它(除了PropertyChanged事件)

+0

非常感谢! 我真的忘了提高PropertyChanging的一些属性!问题出现在我面前,我看不到它:-) – rmoreira