Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        private void AddCartToSupportRequest(ShoppingCart myCart)
        {
            var context = new CrmOrganizationServiceContext(new CrmConnection("Xrm"));

            var supportRequest = new Entity("adx_supportrequest")
            {
                Id = CurrentStepEntityID
            };

            supportRequest.Attributes["adx_shoppingcartid"] = myCart.Entity.ToEntityReference();

            context.Attach(supportRequest);

            context.UpdateObject(supportRequest);

            context.SaveChanges();
        }
Exemplo n.º 3
0
        public static bool BulkSave(EntityCollection entities, CrmConnection connection = null)
        {
            OrganizationService srv = new OrganizationService(connection ?? XrmConnection.Connection);

            using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv))
            {
                foreach (Entity e in entities.Entities)
                {
                    if (e.Id.Equals(Guid.Empty))
                    {
                        e.Id = Guid.NewGuid();
                        // service.Attach(e);
                        service.AddObject(e);
                    }
                    else
                    {
                        service.Attach(e);
                        service.UpdateObject(e);
                    }
                }
                SaveChangesResultCollection result = service.SaveChanges(Microsoft.Xrm.Sdk.Client.SaveChangesOptions.None);
                return(!result.HasError);
            }
        }