Beispiel #1
0
 private void DeleteFromContext(Entity entity)
 {
     if (!_serviceContext.IsAttached(entity))
     {
         _serviceContext.Attach(entity);
     }
     _serviceContext.DeleteObject(entity);
     _serviceContext.SaveChanges();
 }
Beispiel #2
0
        private static void LabUsingLinqDeleteContacts(IOrganizationService service)
        {
            using (var orgContext = new ServiceContext(service))
            {
                var del = from c in orgContext.ContactSet
                          where (c.JobTitle == "Purchasing Supervisor")
                          select c;

                foreach (var a in del)
                {
                    orgContext.DeleteObject(a);
                }

                orgContext.SaveChanges();
            }
        }
Beispiel #3
0
        void OnEntityCollectionPropertyChanged(object entity, IEdmNavigationProperty property, NotifyCollectionChangedEventArgs eventArgs)
        {
            if (!_changeTrackingEnabled)
            {
                return;
            }
            var entityTypeName = entity.GetType().Name;

            if (eventArgs.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (var newItem in eventArgs.NewItems)
                {
                    var newItemTypeName = newItem.GetType().Name;
                    ServiceContext.AddRelatedObject(entity, property.Name, newItem);
                    SetEntityId(newItemTypeName, newItem, Guid.NewGuid());

                    var roles = MetadataProvider.Instance.EntityRelationshipRoles
                                .Where(r => r.Entity.PhysicalName == newItemTypeName &&
                                       r.EntityRelationship.EntityRelationshipRelationships
                                       .Exists(p => p.Relationship.ReferencedEntity.PhysicalName == entityTypeName));
                    foreach (var role in roles)
                    {
                        var rerferancingAttributeName = role.EntityRelationship.EntityRelationshipRelationships[0]
                                                        .Relationship.ReferencingAttribute.PhysicalName;
                        var entityPropertyInfo = newItem.GetType().GetProperty(rerferancingAttributeName);
                        if (entityPropertyInfo != null)
                        {
                            //var entityId = GetEntityId(entityTypeName, entity);
                            //entityPropertyInfo.SetValue(newItem, entityId, null);
                        }
                    }
                }
            }
            else if (eventArgs.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (var oldItem in eventArgs.OldItems)
                {
                    bool isTracked = IsTracked(oldItem);
                    if (isTracked)
                    {
                        //if (!IsAdded(oldItem) && !IsAdded(entity)) ServiceContext.DeleteLink(entity, property.Name, oldItem);
                        ServiceContext.DeleteObject(oldItem);
                    }
                }
            }
        }
Beispiel #4
0
        public void DeleteObject(string entityName, object entity)
        {
            Uri  editLink;
            var  entityType           = GetTypeDefinition(entityName);
            var  entityClrtype        = ResolveType(entityType.Name);
            bool hasIsDeletedProperty = HasField(entityClrtype, "IsDeleted");

            if (ServiceContext.TryGetUri(entity, out editLink))
            {
                if (hasIsDeletedProperty)
                {
                    ServiceContext.UpdateObject(entity);
                    SetFieldValue(entity, "IsDeleted", true);
                }
                else
                {
                    ServiceContext.DeleteObject(entity);
                }
                return;
            }
            var id = GetObjectId(entityName, entity);

            if (hasIsDeletedProperty)
            {
                var entityObject = GetOrNew(entityName, id, null);
                SetFieldValue(entityObject, "IsDeleted", true);
                ServiceContext.UpdateObject(entityObject);
            }
            else
            {
                var entityObject = Activator.CreateInstance(entityClrtype);
                SetEntityId(entityName, entityObject, id);
                var entitySetName = entityName.InflectTo().Pluralized;
                ServiceContext.AttachTo(entitySetName, entityObject);
                ServiceContext.DeleteObject(entityObject);
            }
        }
        public void Notify(Toyota.Tsusho.CRM.API.MessageContracts.OrderNotifyRequestMessage request)
        {
            using (OrganizationServiceProxy proxy = CRMHelper.Connect())
            {
                ServiceContext context = new ServiceContext(proxy);

                foreach (OrderItemDataContract item in request.Items)
                {
                    bool add = false;

                    Entity entity = null;

                    Invoice record = (from a in context.InvoiceSet
                                      where a.new_saporderno == item.Invoice.new_saporderno
                                      select a).FirstOrDefault();

                    if (record == null)
                    {
                        add = true;

                        record = new Invoice();
                    }

                    //CustomerId Lookup

                    Contact contact = (from c in context.ContactSet
                                       where c.new_customeraccountnumber == item.Invoice.CustomerId.Name
                                       select c).FirstOrDefault();

                    if (contact == null)
                        throw new Exception(String.Format("No Contact could be retrieved for {0}", item.Invoice.CustomerId.Name));

                    //SalesOrder

                    SalesOrder order = null;

                    if (item.Invoice.SalesOrderId != null)
                    {
                        order = (from o in context.SalesOrderSet
                                            where o.new_DBMOrderNumber == item.Invoice.SalesOrderId.Name
                                            select o).FirstOrDefault();
                    }

                    //SalesOffice

                    Territory salesOffice = null;

                    if (item.Invoice.new_salesoffice != null)
                    {
                        entity = (from s in context.CreateQuery("territory")
                                     where s["new_sapcode"] == item.Invoice.new_salesoffice.Name
                                     select s).FirstOrDefault();

                        if (entity != null)
                            salesOffice = entity.ToEntity<Territory>();
                    }

                    //Plant

                   entity = (from p in context.CreateQuery("territory")
                                     where p["new_sapcode"] == item.Invoice.new_plant.Name
                                     select p).FirstOrDefault();

                   Territory plant = null;

                    if(entity != null)
                        plant = entity.ToEntity<Territory>();

                    //new_invoicetype

                    new_invoicetype invoiceType = null;

                    if(item.Invoice.new_invoicetype != null)
                    {
                        entity = (from it in context.CreateQuery("new_invoicetype")
                                     where it["new_typeidinvoice"] == item.Invoice.new_invoicetype.Name
                                     select it).FirstOrDefault();

                        if(entity != null)
                            invoiceType = entity.ToEntity<new_invoicetype>();
                    }

                    //Populate Order Fields

                    record.new_saporderno = item.Invoice.new_saporderno;

                    record.new_client = item.Invoice.new_client;

                    if (contact != null)
                        record.CustomerId = contact.ToEntityReference();

                    if (order != null)
                        record.SalesOrderId = order.ToEntityReference();

                    record.new_precedingdocument = item.Invoice.new_precedingdocument;

                    if (salesOffice != null)
                        record.new_salesoffice = salesOffice.ToEntityReference();

                    if(plant != null)
                        record.new_plant = plant.ToEntityReference();

                    record.new_customeradviser = item.Invoice.new_customeradviser;
                    record.new_billingdate = item.Invoice.new_billingdate;
                    record.new_licenseplatenumber = item.Invoice.new_licenseplatenumber;
                    record.new_country = item.Invoice.new_country;
                    record.new_counterreading = item.Invoice.new_counterreading;
                    record.new_counterunit = item.Invoice.new_counterunit;
                    record.new_orderstatus = item.Invoice.new_orderstatus;
                    record.new_netvalue = item.Invoice.new_netvalue;
                    record.new_vehicleguid = item.Invoice.new_vehicleguid;

                    if (invoiceType != null)
                        record.new_invoicetype = invoiceType.ToEntityReference();

                    if (add)
                        context.AddObject(record);
                    else
                        context.UpdateObject(record);

                    //Invoice Detail

                    //We will nowdelete all line item s and readd them
                    //if we do not do this we will get duplicate records.

                    if (record.invoice_details != null)
                    {
                        foreach (InvoiceDetail detail in record.invoice_details)
                            context.DeleteObject(detail);
                    }

                    foreach (InvoiceDetail lineItem in item.InvoiceDetails)
                    {
                        InvoiceDetail detail = new InvoiceDetail();

                        //new_material

                        new_modelsalescode material = null;

                        if(detail.new_material != null)
                        {
                            material = (from m in context.new_modelsalescodeSet
                                            where m.new_name == lineItem.new_material.Name
                                        select m).FirstOrDefault();
                        }

                        //Plant

                        Territory detailPlant = null;

                        if (lineItem.new_plant != null)
                        {
                            entity = (from p in context.CreateQuery("territory")
                                      where p["new_sapcode"] == lineItem.new_plant.Name
                                      select p).FirstOrDefault();

                            if (entity != null)
                                detailPlant = entity.ToEntity<Territory>();
                        }

                        //Populate Invoice Detail

                        detail.LineItemNumber = lineItem.LineItemNumber;
                        detail.new_pricingreferencematerial = lineItem.new_pricingreferencematerial;
                        detail.new_lvhierno = lineItem.new_lvhierno;

                        if (material != null)
                            detail.new_material = material.ToEntityReference();

                        detail.new_materialgroup = lineItem.new_materialgroup;
                        detail.ProductDescription = lineItem.ProductDescription;
                        detail.IsPriceOverridden = lineItem.IsPriceOverridden;
                        detail.IsProductOverridden = lineItem.IsProductOverridden;
                        detail.new_description1 = lineItem.new_description1;
                        detail.new_itemcategory = lineItem.new_itemcategory;
                        detail.new_deleteitem = lineItem.new_deleteitem;
                        detail.Quantity = lineItem.Quantity;
                        detail.new_targetqtyuom = lineItem.new_targetqtyuom;
                        detail.new_baseunit = lineItem.new_baseunit;
                        detail.new_targetqtyuom = lineItem.new_targetqtyuom;
                        detail.new_division = lineItem.new_division;
                        detail.PricePerUnit = lineItem.PricePerUnit;
                        detail.new_salesunit = lineItem.new_salesunit;

                        if (detailPlant != null)
                            detail.new_plant = detailPlant.ToEntityReference();

                        detail.new_storagelocation = lineItem.new_storagelocation;

                        context.AddRelatedObject(record, new Relationship("invoice_details"), detail);
                    }
                }

                context.SaveChanges();
            }
        }
        private void MigrateExistingTimeouts(ServiceContext context)
        {
            var existing = (from c in context.TimeoutData
                          where c.PartitionKey == "TimeoutData"
                          select c).ToList();

            foreach(var timeout in existing)
            {
                TimeoutDataEntity timeoutDataEntity;

                if (!TryGetTimeoutData(context, timeout.Time.ToString(partitionKeyScope), timeout.RowKey, out timeoutDataEntity))
                    context.AddObject(ServiceContext.TimeoutDataEntityTableName,
                                      new TimeoutDataEntity(timeout.Time.ToString(partitionKeyScope), timeout.RowKey)
                                          {
                                              Destination = timeout.Destination,
                                              SagaId = timeout.SagaId,
                                              StateAddress = timeout.RowKey,
                                              Time = timeout.Time,
                                              CorrelationId = timeout.CorrelationId,
                                              OwningTimeoutManager = timeout.OwningTimeoutManager
                                          });

                if (!TryGetTimeoutData(context, timeout.SagaId.ToString(), timeout.RowKey, out timeoutDataEntity))
                    context.AddObject(ServiceContext.TimeoutDataEntityTableName,
                                          new TimeoutDataEntity(timeout.SagaId.ToString(), timeout.RowKey)
                                          {
                                              Destination = timeout.Destination,
                                              SagaId = timeout.SagaId,
                                              StateAddress = timeout.RowKey,
                                              Time = timeout.Time,
                                              CorrelationId = timeout.CorrelationId,
                                              OwningTimeoutManager = timeout.OwningTimeoutManager
                                          });

                if (!TryGetTimeoutData(context, timeout.RowKey, string.Empty, out timeoutDataEntity))
                    context.AddObject(ServiceContext.TimeoutDataEntityTableName,
                                      new TimeoutDataEntity(timeout.RowKey, string.Empty)
                                          {
                                              Destination = timeout.Destination,
                                              SagaId = timeout.SagaId,
                                              StateAddress = timeout.RowKey,
                                              Time = timeout.Time,
                                              CorrelationId = timeout.CorrelationId,
                                              OwningTimeoutManager = timeout.OwningTimeoutManager
                                          });

                context.DeleteObject(timeout);
                context.SaveChanges();
            }
        }
        public bool TryRemove(string timeoutId, out TimeoutData timeoutData)
        {
            timeoutData = null;

            var context = new ServiceContext(account.TableEndpoint.ToString(), account.Credentials);
            try
            {
                TimeoutDataEntity timeoutDataEntity;
                if (!TryGetTimeoutData(context, timeoutId, string.Empty, out timeoutDataEntity))
                {
                    return false;
                }

                timeoutData = new TimeoutData
                    {
                        Destination = Address.Parse(timeoutDataEntity.Destination),
                        SagaId = timeoutDataEntity.SagaId,
                        State = Download(timeoutDataEntity.StateAddress),
                        Time = timeoutDataEntity.Time,
                        CorrelationId = timeoutDataEntity.CorrelationId,
                        Id = timeoutDataEntity.RowKey,
                        OwningTimeoutManager = timeoutDataEntity.OwningTimeoutManager,
                        Headers = Deserialize(timeoutDataEntity.Headers)
                    };

                TimeoutDataEntity timeoutDataEntityBySaga;
                if (TryGetTimeoutData(context, timeoutDataEntity.SagaId.ToString(), timeoutId, out timeoutDataEntityBySaga))
                {
                    context.DeleteObject(timeoutDataEntityBySaga);
                }

                TimeoutDataEntity timeoutDataEntityByTime;
                if (TryGetTimeoutData(context, timeoutDataEntity.Time.ToString(partitionKeyScope), timeoutId, out timeoutDataEntityByTime))
                {
                    context.DeleteObject(timeoutDataEntityByTime);
                }

                RemoveState(timeoutDataEntity.StateAddress);

                context.DeleteObject(timeoutDataEntity);

                context.SaveChanges();
            }
            catch(Exception ex)
            {
                Logger.Debug(string.Format("Failed to clean up timeout {0}", timeoutId), ex);
            }

            return true;
        }
        public void RemoveTimeoutBy(Guid sagaId)
        {
            var context = new ServiceContext(account.TableEndpoint.ToString(), account.Credentials);
            try
            {
                var results = (from c in context.TimeoutData
                               where c.PartitionKey == sagaId.ToString()
                               select c).ToList();

                foreach (var timeoutDataEntityBySaga in results)
                {
                    RemoveState(timeoutDataEntityBySaga.StateAddress);

                    TimeoutDataEntity timeoutDataEntityByTime;
                    if (TryGetTimeoutData(context, timeoutDataEntityBySaga.Time.ToString(partitionKeyScope), timeoutDataEntityBySaga.RowKey, out timeoutDataEntityByTime))
                        context.DeleteObject(timeoutDataEntityByTime);

                    TimeoutDataEntity timeoutDataEntity;
                    if (TryGetTimeoutData(context, timeoutDataEntityBySaga.RowKey, string.Empty, out timeoutDataEntity))
                        context.DeleteObject(timeoutDataEntity);

                    context.DeleteObject(timeoutDataEntityBySaga);
                }
                context.SaveChanges();
            }
            catch(Exception ex)
            {
                Logger.Debug(string.Format("Failed to clean up timeouts for saga {0}", sagaId), ex);
            }
        }
 public void Delete(PerformanceLog item)
 {
     ServiceContext.AttachTo(PerformanceLogContext.PerformanceLogTableName, item);
     ServiceContext.DeleteObject(item);
     ServiceContext.SaveChanges();
 }