Exemplo n.º 1
0
        protected override int ExecuteInsert(IDictionary values)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Begin");

            if (!CanInsert)
            {
                throw new NotSupportedException("Insert not supported.");
            }

            string entityName = values["EntityName"] as string;

            if (string.IsNullOrEmpty(entityName))
            {
                throw new ArgumentException("Insert requires an EntityName to be specified as one of the values.", "values");
            }

            var entity = new Entity(entityName);

            SetEntityAttributes(entity, values);

            var rowsAffected      = 0;
            var insertedEventArgs = new CrmDataSourceViewInsertedEventArgs();

            try
            {
                _crmDataContext.AddObject(entity);
                _crmDataContext.SaveChanges();

                insertedEventArgs.EntityId = entity.Id;

                rowsAffected = 1;
            }
            catch (Exception e)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, e.ToString());

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

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("End: rowsAffected={0}", rowsAffected));

            OnInserted(insertedEventArgs);

            return(rowsAffected);
        }
Exemplo n.º 2
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);
            }
        }