protected virtual void OnUpdated(CrmDataSourceViewUpdatedEventArgs args)
        {
            var handler = (EventHandler <CrmDataSourceViewUpdatedEventArgs>)Events[EventUpdated];

            if (handler != null)
            {
                handler(this, args);
            }
        }
        protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteUpdate", "Begin");

            if (!CanUpdate)
            {
                throw new NotSupportedException("Update not supported.");
            }

            var id         = keys["ID"] as Guid?;
            var entityName = keys["Name"] as string;

            if (id == null || entityName == null)
            {
                throw new ArgumentException("Update requires the 'ID' and 'Name' to be specified as DataKeyNames.", "keys");
            }

            var rowsAffected     = 0;
            var updatedEventArgs = new CrmDataSourceViewUpdatedEventArgs();

            try
            {
                var entity = _crmDataContext.Retrieve(entityName, id.Value, new ColumnSet(true));

                if (entity == null)
                {
                    throw new NullReferenceException("The {0} entity with ID='{1}' could not be found.".FormatWith(id, entityName));
                }

                SetEntityAttributes(entity, values);

                _crmDataContext.Attach(entity);

                _crmDataContext.UpdateObject(entity);

                var result = _crmDataContext.SaveChanges();

                if (result.HasError)
                {
                    rowsAffected = 0;
                }
                else
                {
                    updatedEventArgs.Entity = entity;
                    rowsAffected            = 1;
                }
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmDataSourceView", "ExecuteUpdate", "{0}", e);

                updatedEventArgs.Exception        = e;
                updatedEventArgs.ExceptionHandled = true;
            }

            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteUpdate", "End: rowsAffected={0}", rowsAffected);

            OnUpdated(updatedEventArgs);

            return(rowsAffected);
        }