none
updating disconnected collections RRS feed

  • General discussion

  • Very standart scenario example: I have Order entity and it has OrderLine entity list. How do i handle changes in OrderLine collection in disconnected scenario? I mean specific caase i could handle, but what about generic way, may be someone has nice routine for this.

    Thursday, June 10, 2010 12:30 PM

All replies

  • public static class Extensions
    {
    public static bool AttachObject(this DataContext datacontext, object entity)
    {
    	bool success = false;
    	ITable table = datacontext.GetTable(entity.GetType());
    
    	if ( table != null )
    	{
    		table.Attach(entity);
    		success = true;
    	}
    	return success;
    }
    
    public static bool AttachObject(this DataContext datacontext, object modifiedEntity, object originalEntity)
    {
    	bool success = false;
    	ITable table = datacontext.GetTable(entity.GetType());
    
    	if ( table != null )
    	{
    		table.Attach(modifiedEntity, originalEntity);
    		success = true;
    	}
    	return success;
    }
    }
    
    Then you can simply use the namespace the extension class is in and do DataContext.AttachObject(object); via extension.  Probably would want to catch whatever Exceptions Attach throws, since it does throw a few exceptions.
    Thursday, June 10, 2010 8:45 PM
  • Well, that's just attach, so it covers just updated entities in collection, but you need to take care also of added and deleted entities and most interesting part is how everything fits together.
    Friday, June 11, 2010 5:40 AM