none
What is best practices to synchronize child entities of the parrent in case of disconnected mode? RRS feed

  • Question

  • Hi,

    Can somebody help me with link to some manual how to synchronize child entities of the parrent in case of disconnected mode?

    I've checked the

    Add/Attach and Entity States

    Automatic Detect Changes

    Working with Property Values

    but still cannot find answer.

    Guess I have two entities:

    public class ParentEntity
    {
    	public Guid Id { set; get; }
    	public string ParentName { set; get; }
    
    	public virtual ICollection<ChildEntity> Childs { set; get; }
    }
    
    public class ChildEntity
    {
    	public Guid Id { set; get; }
    	public string ChildName { set; get; }
    	public Guid ParentId { set; get; }
    
    	public ParentEntity Parent { set; get; }
    }

    and have following code:

    ParentEntity parent;
    
    using (ctx = new SomeEntityContext())
    {
    	parent = ctx.Parents.Where(parentSearch => parentSearch.ParentName == "Some Parent")
    		.Include(parentSearch => parentSearch.Children).FirstOrDefault();
    }
    
    // Now we are disconnected from the context...
    // For example in UI level of n-tier
    
    // Create new child
    ChildEntity child = new ChildEntity();
    child.Id = Guid.NewGuid();
    child.ChildName = "Child #2";
    child.ParentId = parent.Id;
    parent.Childs.Add(child);
    
    // Remove existing child
    parent.Children.Remove(parent.Children.FirstOrDefault(childSearch => child.ChildName == "Child #1"));
    
    
    // Now we are again at server and need to save the changes
    using (ctx = new SomeEntityContext())
    {
    	ctx.Parents.Attach(parent);
    
    	// Detect changes for the parrent
    	ctx.Entry(parent).OriginalValues.SetValues(ctx.Entry(parent).GetDatabaseValues());
    
    	// TODO: to do something to detect changes for children.
    	// Should I load it from the database and detect manually
    	// which childs were added which modified and which deleted?
    }

    So is there some way to detect changes for children other than load current children from the database and compare it manually?

    Thanks in advice.



    Friday, November 30, 2012 4:51 PM

Answers

All replies