我是Windows Phone 7开发的新手,我遇到了Linq to SQL的问题。我正在尝试更新一个对象,但它只是不起作用。我得到了我想要更新的对象并修改了我想要的属性,但是当我调用SaveChanges时,它没有在数据库上更新数据。
我已经使用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 :(得分:0)
似乎您没有为PropertyChanging
属性举起Saida
事件;此事件对于LINQ-to-SQL很重要,因此我建议更新所有代表列的成员以提升它(除了PropertyChanged
事件)