protected virtual void OnDeleted(CrmDataSourceViewDeletedEventArgs args)
        {
            var handler = (EventHandler <CrmDataSourceViewDeletedEventArgs>)Events[EventDeleted];

            if (handler != null)
            {
                handler(this, args);
            }
        }
        protected override int ExecuteDelete(IDictionary keys, IDictionary oldValues)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Begin");

            if (!CanDelete)
            {
                throw new NotSupportedException("Delete not supported.");
            }

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

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

            var rowsAffected     = 0;
            var deletedEventArgs = new CrmDataSourceViewDeletedEventArgs();

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

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

                _crmDataContext.Attach(entity);

                _crmDataContext.DeleteObject(entity);

                var result = _crmDataContext.SaveChanges();

                rowsAffected = result.HasError ? 0 : 1;
            }
            catch (Exception e)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, e.ToString());

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

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "End");

            OnDeleted(deletedEventArgs);

            return(rowsAffected);
        }