S
S
Smilley2016-11-21 03:19:58
C++ / C#
Smilley, 2016-11-21 03:19:58

How to update a record in a database using EF from a setter?

Hello. Can you please tell me how to correctly update a record in the database from Grid? I'm trying to do this from a property setter, but I keep getting into an infinite loop:

private string _comments;
public string Comments
{
    get { return _comments; }
    set
    {
        if (_comments != value)
        {
            this._comments = value;
            using (var db = new DataContext())
            {
                //var chbk = new ChargebackAnalyzeModel() {ID = this.ID, Comments = value};
                db.Chargebacks.Attach(this);
                db.Entry(this).Property(x => x.Comments).IsModified = true;
                db.SaveChangesAsync();
            }
            OnPropertyChanged();
        }
    }
}

I tried to do it in different ways, but it does not work, unfortunately. Value is updated from Grid'a.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kuznetsov, 2016-11-21
@DarkRaven

I think the problem is here:

db.Chargebacks.Attach(this);
db.Entry(this).Property(x => x.Comments).IsModified = true;

Try instead of this to create a "copy", "clone" of this entity above and already work with it.
In general, if I were you, I would look for a different approach - starting from the data change events of the column / row of the grid, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question