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)
        {
            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteDelete", "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}' could not be found.".FormatWith(id, entityName));
                }

                _crmDataContext.Attach(entity);

                _crmDataContext.DeleteObject(entity);

                var result = _crmDataContext.SaveChanges();

                rowsAffected = result.HasError ? 0 : 1;
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmDataSourceView", "ExecuteDelete", "{0}", e);

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

            Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteDelete", "End");

            OnDeleted(deletedEventArgs);

            return(rowsAffected);
        }