M
M
Mikhail2018-02-02 14:51:52
C++ / C#
Mikhail, 2018-02-02 14:51:52

Why can't update one field in EF?

There is such a model:

public class Phone
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Company { get; set; }
        public int Price { get; set; }
    }

I'm trying to update the Price field on a phone with Id = 2:
Phone Phone = new Phone { Id = 2};
this.Db.Entry(Phone).Property<int>("Price").CurrentValue = 666;
await this.Db.SaveChangesAsync();

No error occurs, but the field value does not change. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-02-02
@mak_ufo

You attached it to the context, but there is no update. Try to explicitly indicate that the model has been modified

this.Db.Entry(Phone).State = EntityState.Modified;
this.Db.SaveChanges();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question