There are entities derived out of context. They need to be updated in the database (if there are changes).

To update the record, you need to tighten its copy from the database, and then compare all the fields. And if the contents of the fields are different, set EntityState.Modified .

The question is compared fields.
Now I compare through redefined Equals for models. But can there be a standard Entity Framework for this purpose? Constantly update Equals , if the models have changed, I do not want to. It would be great if the context.Users.Update(user) could be done so and he himself compared everything.

    1 answer 1

    Option 1. Look, is this code appropriate?

    If you are an individual, you’ll be able to make it. For example:

     var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" }; using (var context = new BloggingContext()) { context.Entry(existingBlog).State = EntityState.Modified; // Do some more work... context.SaveChanges(); } 

    Option 2. If you are too lazy to constantly update Equals , you can use reflection.

      foreach (PropertyInfo property in this.GetType().GetProperties()) { object value1 = property.GetValue(this, null); object value2 = property.GetValue(another, null); if (!value1.Equals(value2)) { return false; } } return true; 

    Option 3. Again, if you are too lazy to constantly update Equals , and the reflection seems to be slow - try Resharper, it will quickly generate Equals methods.