private static void InitializeTableSchemaFromEntity(CloudTableClient tableStorage, string entityName, TableServiceEntity entity)
        {
            TableServiceContext context = tableStorage.GetDataServiceContext();
            DateTime            now     = DateTime.UtcNow;

            entity.PartitionKey = Guid.NewGuid().ToString();
            entity.RowKey       = Guid.NewGuid().ToString();
            Array.ForEach(entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance), p =>
            {
                if ((p.Name != "PartitionKey") && (p.Name != "RowKey") && (p.Name != "Timestamp"))
                {
                    if (p.PropertyType == typeof(string))
                    {
                        p.SetValue(entity, Guid.NewGuid().ToString(), null);
                    }
                    else if (p.PropertyType == typeof(DateTime))
                    {
                        p.SetValue(entity, now, null);
                    }
                }
            });

            context.AddObject(entityName, entity);
            context.SaveChangesWithRetries();
            context.DeleteObject(entity);
            context.SaveChangesWithRetries();
        }
예제 #2
0
        private static void AddData(TableServiceContext tableContext, string p)
        {
            CustomerEntity customerEntity1 = new CustomerEntity("Jusel", "Jaroslaw");
            customerEntity1.Email = "*****@*****.**";
            customerEntity1.PhoneNumber = "+370xxxxx1";

            tableContext.AddObject("sampleTable", customerEntity1);

            CustomerEntity customerEntity2 = new CustomerEntity("Ungurys", "Andrius");
            customerEntity2.Email = "*****@*****.**";
            customerEntity2.PhoneNumber = "+370xxxxx2";

            tableContext.AddObject("sampleTable", customerEntity2);

            CustomerEntity customerEntity3 = new CustomerEntity("Čiukšys", "Vytautas");
            customerEntity3.Email = "*****@*****.**";
            customerEntity3.PhoneNumber = "+370xxxxx3";

            tableContext.AddObject("sampleTable", customerEntity3);

            CustomerEntity customerEntity4 = new CustomerEntity("Norvaišaitė", "Modesta");
            customerEntity4.Email = "*****@*****.**";
            customerEntity4.PhoneNumber = "+370xxxxx4";

            tableContext.AddObject("sampleTable", customerEntity4);

            CustomerEntity customerEntity5 = new CustomerEntity("Vaicekauskas", "Vilius");
            customerEntity5.Email = "*****@*****.**";
            customerEntity5.PhoneNumber = "+370xxxxx5";

            tableContext.AddObject("sampleTable", customerEntity5);

            CustomerEntity customerEntity6 = new CustomerEntity("Stašys", "Laurynas");
            customerEntity6.Email = "*****@*****.**";
            customerEntity6.PhoneNumber = "+370xxxxx6";

            tableContext.AddObject("sampleTable", customerEntity6);

            //Finally, changes are saved
            tableContext.SaveChangesWithRetries();
        }
        private static void ReleaseItemExclusive(TableServiceContext svc, SessionRow session, object lockId)
        {
            if ((int)lockId != session.Lock)
            {
                // obviously that can happen, but let's see when at least in Debug mode
                Debug.Assert(false, "Error");
                return;
            }

            session.ExpiresUtc = DateTime.UtcNow.AddMinutes(session.Timeout);
            session.Locked = false;
            svc.UpdateObject(session);
            svc.SaveChangesWithRetries();
        }
        private MembershipUser ProcessGetUserQuery(TableServiceContext svc, IQueryable<MembershipRow> query, bool updateLastActivityDate)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            // if no user is found, we return null
            MembershipUser res = null;

            // the GetUser query should return at most 1 result, we do a Take(2) to detect error conditions
            query = query.Take(2);

            IEnumerable<MembershipRow> queryResult = query.AsTableServiceQuery().Execute();
            if (queryResult == null)
            {
                return null;
            }

            var l = new List<MembershipRow>(queryResult);
            if (l.Count == 0)
            {
                return null;
            }
            else if (l.Count > 1)
            {
                throw new ProviderException("Non-unique primary keys!");
            }
            else
            {
                MembershipRow row = l.First();
                if (updateLastActivityDate)
                {
                    row.LastActivityDateUtc = DateTime.UtcNow;
                }

                res = new MembershipUser(
                                         this.Name,
                                         row.UserName,
                                         row.UserId,
                                         row.Email,
                                         row.PasswordQuestion,
                                         row.Comment,
                                         row.IsApproved,
                                         row.IsLockedOut,
                                         row.CreateDateUtc.ToLocalTime(),
                                         row.LastLoginDateUtc.ToLocalTime(),
                                         row.LastActivityDateUtc.ToLocalTime(),
                                         row.LastPasswordChangedDateUtc.ToLocalTime(),
                                         row.LastLockoutDateUtc.ToLocalTime());

                if (updateLastActivityDate)
                {
                    svc.UpdateObject(row);
                    svc.SaveChangesWithRetries();
                }
            }

            return res;
        }
        private static void ReleaseItemExclusive(TableServiceContext svc, SessionRow session, object lockId)
        {
            if ((int) lockId != session.Lock)
            {
                return;
            }

            session.ExpiresUtc = DateTime.UtcNow.AddMinutes(session.Timeout);
            session.Locked = false;
            svc.UpdateObject(session);
            svc.SaveChangesWithRetries();
        }
예제 #6
0
        public void SaveFriendLikes()
        {
            // Persist friend likes to Table Storage

            var context = new TableServiceContext(tableClient.BaseUri.ToString(), tableClient.Credentials);

            // Using the Upsert pattern in the August 2011 API / November 2011 SDK (1.6)
            var n = 0;
            foreach (var k in FriendLikes.Keys)
            {
                context.AttachTo(FRIEND_LIKES_TABLE, FriendLikes[k]);
                context.UpdateObject(FriendLikes[k]);
                if (n++ % 100 == 0)
                {
                    context.SaveChangesWithRetries(SaveChangesOptions.Batch|SaveChangesOptions.ReplaceOnUpdate);
                }
            }

            // Create the pre-calculated JSON Blob

            var blob = blobContainer.GetBlobReference(UserId);
            var likes = GetOrderedFriendLikes();
            var json = JsonConvert.SerializeObject(likes);
            blob.UploadText("dataCallback(" + json + ")");
        }