none
How to set a relationship as modified RRS feed

  • Question

  • Hi,

    I'm trying to avoid load an entire entity to update only two fields, so I would like to set these fields as modified and update the entity.

    It's like this:

                var referral=new Referral() {Id=referralId, Status=satus, RejectionReason = reason};

                myContext.Attach(referral);

                var entry = myContext.Entry(referral);
                entry.Property(r => r.RejectionReason).IsModified = true;

                myContext.SaveChanges();


    However, how can I set the property Status as modified?

    • Using the property method doesn't work, because it's a relationship with a status table
    • Using the property Reference doesn't work, because there isn't a modified status, only an added status.
               

    Is there a solution for this?

    Regards,

    Dennes



    • Edited by Dennes Tuesday, October 23, 2018 3:12 PM
    Tuesday, October 23, 2018 3:11 PM

All replies

  • Hi Dennes,

    >>Is there a solution for this?

    According to your description, it seems that you want to change relationship tables, it seems that we could use ChangeRelationshipState method to achieve it, for more information, please refer to:

    https://docs.microsoft.com/en-us/ef/ef6/fundamentals/relationships

    And Here is a link which share a sample code for your reference.

    https://stackoverflow.com/questions/43311094/the-property-method-can-only-be-used-with-primitive-or-complex-properties-use-t

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 24, 2018 2:38 AM
    Moderator
  • Hi, Zhanlong,

    Good links! I tried to use ChangeRelationshipState, but it requires to change in both sides, and on the other side I don't have a relation property.

    On the example, Referral object has a status property but status object hasn't a property with a collection of referrals.

    Regards,

    Dennes



    • Edited by Dennes Thursday, October 25, 2018 11:40 AM
    Thursday, October 25, 2018 11:40 AM
  • Hi Dennes,

    If you don't have a relation property, I would suggest that you could modify them Individually. 

    Best regards,
    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 26, 2018 1:31 AM
    Moderator