簡體   English   中英

Linq-To-SQL無法在WP7上更新

[英]Linq-To-SQL not updating on WP7

我是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。希望您能對我有所幫助:-)謝謝!

您似乎沒有為Saida財產引發PropertyChanging事件。 此事件對於LINQ-to-SQL很重要,因此建議您更新代表列的所有成員以引發此事件(除了PropertyChanged事件)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM