예제 #1
0
        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();
        }
        /// <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)
            {
                TableServiceContext ctx = tableStorage.GetDataServiceContext();

                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);
                    }
                }
            }
        }
예제 #3
0
 public void Save(TableServiceEntity entity)
 {
     new AzureTable <TableServiceEntity>(this.account, this.tableName).Add(entity);
 }
예제 #4
0
 public void Delete(TableServiceEntity entity)
 {
     new AzureTable <TableServiceEntity>(this.account, this.tableName).Delete(entity);
 }
 public BaseModel(TableServiceEntity entity)
 {
     this.Entity = entity;
 }
예제 #6
0
 public void UpdateEntity(TableServiceEntity entity)
 {
     this.Server.RawContext.UpdateObject(entity);
 }
예제 #7
0
 public void AddEntity(TableServiceEntity entity)
 {
     this.Server.RawContext.AddObject(this.Name, entity);
 }