0

我是 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。我希望你能帮助我 :-) 谢谢你们!

4

1 回答 1

0

您似乎没有为该物业PropertyChanging举办活动;Saida此事件对于 LINQ-to-SQL 很重要,因此我建议更新所有代表列的成员以引发它(除了PropertyChanged事件)

于 2012-06-17T08:59:29.073 回答