0

I'm new to Windows Phone 7 development and I'm in trouble with Linq to SQL. I'm trying to update an object but it just don't work. I get the object I want to update and modify the property I want, but when I call SaveChanges, the data it's not updated on database.

I already downloaded the database using ISETool and checked that data isn't updated at all.

What's strange is the querying and inserting methods works fine, but I don't know why updating it's not.

Here's the entity and the updating method code:

[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();

I also checked the GetChangeSet().Updates.Count(), but it's always 0. I hope you can help me :-) thank you guys!

1 Answers1

0

It appears to be the case that you're not raising the PropertyChanging event for the Saida property; this event is important for LINQ-to-SQL, so I'd suggest updating all of your members that represent columns to raise it (in addition to the PropertyChanged event)

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Thank you very much! I really forgot to raise PropertyChanging in some properties! The problem was in front of me and I couldn't see it :-) – rmoreira Jun 17 '12 at 11:46