示例#1
0
        public static void DeleteEntity(TableServiceEntity entity, string tableName)
        {
            var tableServiceContext = GetTableServiceContext(tableName);

            tableServiceContext.DeleteObject(entity);

            tableServiceContext.SaveChangesWithRetries();
        }
示例#2
0
        public static void AddEntity(TableServiceEntity entity, string tableName)
        {
            var tableServiceContext = GetTableServiceContext(tableName);

            // Add the new customer to the people table
            tableServiceContext.AddObject(tableName, entity);

            // Submit the operation to the table service
            tableServiceContext.SaveChangesWithRetries();
        }
示例#3
0
        /// <summary>
        /// Creates tables from a DataServiceContext derived class.
        /// </summary>
        /// <param name="serviceContextType">A DataServiceContext derived class that defines the table schemas.</param>
        /// <param name="baseAddress">The baseAddress of the table storage endpoint.</param>
        /// <param name="credentials">Storage account credentials.</param>
        /// <remarks>
        /// For each table, the class exposes one or more IQueryable&lt;T&gt; properties, where T is a
        /// TableServiceEntity derived class with the required schema.
        /// </remarks>
        public static void CreateTablesFromModel(Type serviceContextType, string baseAddress, StorageCredentials credentials)
        {
            CloudTableClient.CreateTablesFromModel(serviceContextType, baseAddress, credentials);

            CloudTableClient tableStorage = new CloudTableClient(baseAddress, credentials);

            // Execute conditionally for development storage only
            if (tableStorage.BaseUri.IsLoopback)
            {
                var properties = serviceContextType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var table in properties.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(IQueryable <>)))
                {
                    TableServiceEntity entity = Activator.CreateInstance(table.PropertyType.GetGenericArguments()[0]) as TableServiceEntity;
                    if (entity != null)
                    {
                        InitializeTableSchemaFromEntity(tableStorage, table.Name, entity);
                    }
                }
            }
        }
        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();
        }
示例#5
0
        void InsertOrReplace(CloudTableClient tables, string tableName)
        {
            var context = GetTableServiceContext(tables);

            var es = new TableServiceEntity[] {
                new EntityOne("001","000","Apple II", "レインボーなやつ"), // replace
                new EntityOne("001","010","Apple IIc", "白かった"), // insert
                new EntityOneSub(data[2]), // replace
                new EntityOne("001","003", null, null), // replace
            };

            // Updateの時は、Etagが*あるいは読み込んだ時のEtagをのIf-Match ヘッダーを付ける。
            // Inset Or Replaceのときは、If-Matchヘッダー自体を付けない

            // したがって、Attachして、SendingRequest の時にIf-Matchヘッダーを削除する
            foreach (var e in es)
            {
                context.AttachTo(tableName, e);
                context.UpdateObject(e);
            }

            //
            context.SendingRequest += (sender, args) => {
                var request = args.Request as HttpWebRequest;
                logger.Debug(m => m("Method:{0}, If-Match:{1}, Uri:{2}", request.Method, request.Headers["If-Match"], request.RequestUri));
            };

            // Insert Or Replace の場合は、MethodがPUT、SaveChangesOptions.ReplaceOnUpdateを指定するとPUTになる
            var option = SaveChangesOptions.ReplaceOnUpdate | SaveChangesOptions.Batch;
            context.SaveChangesWithRetries(option);

            DumpContext("Dump TableServiceContext", context);
        }