none
Update only partial properties of an entity RRS feed

  • Question

  • I only want to update one property of my entity not the whole entity properties.

    There is already a solution: http://stackoverflow.com/questions/10257360/how-to-update-not-every-fields-of-an-object-using-entity-framework-and-entitysta

    But somehow it does not work for me:

        foreach (var customer in customers)
        {
    var entry = _context.Entry(customer);
    entry.State = EntityState.Modified;

    entry.Property(e => e.x2).IsModified = false;
    entry.Property(e => e.x3).IsModified = false;

        }
        _context.SaveChanges();

        public class Customer
        {
         public string x1 {get; set;}
         public string x2 {get; set;}
         public string x3 {get; set;}
        }

    I only want to update property x1.

    But on SaveChanges() I get this exception:

    **Entity of type Customer in state modified has the following validation errors: field x2 is require.**

    Why is the original value for x2 not taken instead?

    Something I must do wrong.

    I am using .NET 4.5.1 and EF 6.0.2.

                           
    Tuesday, December 17, 2013 1:20 PM

All replies

  • Hi,

    Try to atach your model first. Take a look at this other thread:

    EntityFramework update partial model

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Edited by JA Reyes Tuesday, December 17, 2013 2:11 PM Link
    Tuesday, December 17, 2013 1:32 PM
  • I know this SO thread. There is nothing about attaching the model. Above all when I do this:

    entry.State = EntityState.Modified; then it is attached automatically.

    Tuesday, December 17, 2013 2:05 PM
  • Hello peluca],

    What model have you used, database first, model first or code first?

    For the entity “Customer”, which filed is the key filed?

    >>I only want to update property x1.

    Have a try to use codes entry.Property(e => e.x1).IsModified = true instead of setting the whole entity to be modified.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred BaoModerator Friday, December 27, 2013 9:49 AM
    • Unmarked as answer by __BN__ Friday, December 27, 2013 10:54 PM
    Wednesday, December 18, 2013 2:28 AM
    Moderator
  • Sorry Fred for returning to you. I was on holidays.

    Thats my prod code:

    foreach (var period in periods)
                {
                    _context.Periods.Attach(period);
                    _context.Entry(period).Property(x => x.Content).IsModified = true;
                }
                _context.SaveChanges();

    I get entity error validations that another field must not be null. And thats true the other field of the period entity is null. But I do not have this data available and I do not want to retrieve this data again...

    I just want to tell EF take this modified property, update it and preserve the other original values.

    Is this possible or not with EF?

    In a scenario where you deal with DTOs sent to the client you do not want to send everything back/forth only the properties which can be changed for the rest properties you want to keep the original values.

    Thats my scenario. Not so unusual...

    After googling a while I did this:

    myContext.Configuration.ValidateOnSaveEnabled = false


    Now I could save my partial data, but when I retrieve the period entity again some original values were overwritten with e.g. default value for integer "0" thats bad.

    In my opinion the EF team should create kind of Update<T>(t => t.property == true) or UpdatePartial<T>(...) methods on the DbSet to handle update scenarios more convenient.

    • Edited by __BN__ Friday, December 27, 2013 11:21 PM
    Friday, December 27, 2013 11:04 PM