Exemplo n.º 1
0
        /// <summary>
        /// Defaults the id an any entity created using the Dictionary string, without an already defined Guid, to the default Id.  Doesn't actually create the Entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void DefaultIdForEntity(Entity entity)
        {
            Queue <Guid> ids;

            if (entity.Id != Guid.Empty || !NewEntityDefaultIds.TryGetValue(entity.LogicalName, out ids))
            {
                return;
            }
            if (ids.Count == 0)
            {
                throw new Exception(
                          $"An attempt was made to create an entity of type {entity.LogicalName}, but no id exists in the NewEntityDefaultIds Collection for it.{Environment.NewLine}" +
                          "Either the entity's Id was not populated as a part of initialization, or a call is needs to be added to to OrganizationServiceBuilder.WithIdsDefaultedForCreate(id)");
            }
            entity.Id = ids.Dequeue();
        }
        /// <summary>
        /// Defaults the id an any entity created using the Dictionary string, without an already defined Guid, to the default Id.  Doesn't actually create the Entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void DefaultIdForEntity(Entity entity)
        {
            // throw an error if from context, and NewEntityThrowErrorOnContextCreation is true, and not in the ids collection
            if (entity.Id != Guid.Empty)
            {
                if (entity.EntityState != EntityState.Created)
                {
                    return;
                }

                var hasIds = NewEntityDefaultIds.TryGetValue(entity.LogicalName, out Queue <Guid> ids);

                if (NewEntityThrowErrorOnContextCreation)
                {
                    if (!hasIds || !ContainsAndRemoved(ids, entity.Id))
                    {
                        throw new Exception(
                                  $"An attempt was made to create an entity of type {entity.LogicalName} with the EntityState set to created which normally means it comes from an OrganizationServiceContext.SaveChanges call.{Environment.NewLine}"
                                  +
                                  "Either set ignoreContextCreation to true on the WithIdsDefaultedForCreate call, or define the id before calling SaveChanges, and add the id with the WithIdsDefaultedForCreate method.");
                    }
                }
                else if (hasIds)
                {
                    ContainsAndRemoved(ids, entity.Id);
                }
            }
            else
            {
                if (!NewEntityDefaultIds.TryGetValue(entity.LogicalName, out Queue <Guid> ids))
                {
                    return;
                }
                if (ids.Count == 0)
                {
                    throw new Exception(
                              $"An attempt was made to create an entity of type {entity.LogicalName}, but no id exists in the NewEntityDefaultIds Collection for it.{Environment.NewLine}" +
                              "Either the entity's Id was not populated as a part of initialization, or a call is needs to be added to to OrganizationServiceBuilder.WithIdsDefaultedForCreate(id)");
                }
                entity.Id = ids.Dequeue();
            }
        }