예제 #1
0
        /// <summary>
        /// Создает счетчик последовательных идентификаторов.
        /// </summary>
        /// <param name="context"> Контекст сервиса данных </param>
        /// <param name="id"> Идентификатор счетчика </param>
        public static void CreateIfNotExist(DataServiceContext context,
            string id)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (string.IsNullOrEmpty(id))
                throw new ArgumentNullException("id");

            if (null == GetById(context, id))
            {
                Identity identity = new Identity
                {
                    PartitionKey = id,
                    RowKey = string.Empty,
                    Value = 0
                };
                context.AddObject(Table.Identities, identity);
                context.SaveChanges();
            }
        }
예제 #2
0
        public static void Update(DataServiceContext context, Identity identity)
        {
            if (null == context)
                throw new ArgumentNullException("context");
            if (null == identity)
                throw new ArgumentNullException("identity");

            MergeOption mergeOption = context.MergeOption;
            context.MergeOption = MergeOption.PreserveChanges;
            bool preconditionFailed;
            do
            {
                try
                {
                    preconditionFailed = false;
                    context.UpdateObject(identity);
                    context.SaveChanges();
                }
                catch (DataServiceRequestException e)
                {
                    if (e.Response.First().StatusCode
                        == (int)HttpStatusCode.PreconditionFailed)
                    {
                        preconditionFailed = true;
                        identity = GetById(context, identity.PartitionKey);
                    }
                    else
                        throw;
                }
            }
            while (preconditionFailed);
            context.MergeOption = mergeOption;
        }