1

I have a method that aims to update a Monitor Profile's Nodes and Attributes:

All of the primitive type of the entity (ProfileName, DisplayName) get updated fine. However, on save changes, the Attributes and Nodes do not get modified.

Here is the entity itself:

public class NWatchMonProfile
{

    public NWatchMonProfile()
    {
        this.CasAttributes = new HashSet<NWatchAttribute>();
    }

    [MaxLength(50)]
    public string ProfileName { get; set; }

    public System.DateTime CreatedDate { get; set; }

    [MaxLength(50)]
    public string CreatedUser { get; set; }

    public Nullable<System.DateTime> EndedDate { get; set; }

    [MaxLength(50)]
    public string EndedUser { get; set; }

    public virtual ICollection<NWatchAttribute> CasAttributes { get; set; }

    public virtual ICollection<NWatchNode> Nodes { get; set; }

}

Here is the MVC controller code that is being used to attempt to Update the record.

var monProfile = new NWatchMonProfile
                {
                    Id = profileId,
                    ProfileName = profileName,
                    DisplayName = profileName,
                    CasAttributes = attributes,
                    Nodes = nodes
                };

                monProfile.CreatedDate = DateTime.Now;
                monProfile.DisplayName = profileName;
                monProfile.ProfileName = profileName;
                if (nodes.Count > 0)
                {
                    monProfile.Nodes = nodes;
                }

                monProfile.CasAttributes = attributes;

                var entry = dbContext.Entry(monProfile);
                entry.State = EntityState.Modified;
                dbContext.SaveChanges();

Note: I have verified that the "attributes" array contains a list of NWatchAttributes. STE is no longer recommended by Microsoft.

blgrnboy
  • 4,377
  • 8
  • 37
  • 88
  • 1
    `entry.State = EntityState.Modified` only affects the `entry` itself, not its navigation properties. – Gert Arnold Apr 27 '15 at 20:35
  • 1
    possible duplicate of [Entity Framework not saving modified children](http://stackoverflow.com/questions/18054798/entity-framework-not-saving-modified-children) – Jeroen Vannevel Apr 27 '15 at 20:40
  • I read the related post and it mentioned self tracking entities. Further research said that Microsoft no longer recommends this. So, the question is, what is the best way to deal with this? – blgrnboy Apr 27 '15 at 20:56
  • You could look into GraphDiff https://github.com/refactorthis/GraphDiff – AllMadHare Apr 28 '15 at 00:04
  • Have you tried setting `entry.State = EntityState.Modified` before setting the collection properties (e.g. `monProfile.Nodes = nodes`)? That way, EF could handle tracking your later changes. – jjj Apr 28 '15 at 19:42

1 Answers1

0

Try this:

dbContext.Entry(monProfile.Nodes).State = EntityState.Modified;
dbContext.Entry(monProfile.CasAttributes).State = EntityState.Modified;
dbContext.Entry(monProfile).State = EntityState.Modified;

dbContext.SaveChanges();
JGr.
  • 3
  • 4