locked
Create Entity without a model at design time - Cloud Table RRS feed

  • Question

  • I have an Azure storage container that I'm using Cloud Table on.

    The application I'm building pulls information from several 3rd party Azure Storage Tables (With permission from them as we are working collectively on this project).

    Although each source has related information, they all put data in their tables differently. Is there any way to get information from a table without having a pre-defined model at design time? I would like to create this entity model at runtime. Also, is there a way to query (read) the column headers at runtime without a pre-defined entity model?


    Owner, Quilnet Solutions

    Monday, August 12, 2013 1:51 PM

Answers

  • Of course, please refer to :

    Reading and Saving table storage entities without knowing the schema or updating TableStorageEntity schema at runtime

    Any question please let me know.

    Update:

    The blog I post obsolete. 

    For storage 2.0 there are another way to do that.

    You can use DynamicTableEntity which is in Microsoft.WindowsAzure.Storage.Table Namespace put all your properties in DynamicTableEntity.Properties property.

    Please see my simple code

    protected void Page_Load(object sender, EventArgs e)
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        ConfigurationManager.AppSettings["StorageConnectionString"]);
    
                var tableClient = storageAccount.CreateCloudTableClient();
          
                CloudTable table=tableClient.GetTableReference("Logs");
                var query = new TableQuery();
                var TableResults = table.ExecuteQuery(query);
                DataTable propertiesTable = new DataTable("LogTable");
                DataColumn partitionKeyColumn = new DataColumn();
                partitionKeyColumn.DataType = System.Type.GetType("System.String");
                partitionKeyColumn.ColumnName = "Partition Key";
                propertiesTable.Columns.Add(partitionKeyColumn);
    
                foreach (var result in TableResults)
    	        {
                    DataRow row;
                    row = propertiesTable.NewRow();
                    row["Partition Key"] = result.PartitionKey;
                    if (result.Properties!=null)
                    {
                        foreach (var kvp in result.Properties)
                        {
                            //Add column if not exsit first
                            if (!propertiesTable.Columns.Contains(kvp.Key))
                            {
                                DataColumn Column = new DataColumn();
                                Column.ColumnName = kvp.Key;
                                Column.DataType = System.Type.GetType("System.String");
                                propertiesTable.Columns.Add(Column);
                            }
                            //TODO: Should not use StringValue directly, Should create a method get kvp's string value.
                            //This only work for String Type will cause error in other type.
                            row[kvp.Key] = kvp.Value.StringValue;
                        }
                    }
                    propertiesTable.Rows.Add(row);
    	        }
                GridView1.DataSource = propertiesTable;
                GridView1.DataBind();
          
            }


    Please mark post as answered if it helped!



    Tuesday, August 13, 2013 2:15 AM

All replies

  • Of course, please refer to :

    Reading and Saving table storage entities without knowing the schema or updating TableStorageEntity schema at runtime

    Any question please let me know.

    Update:

    The blog I post obsolete. 

    For storage 2.0 there are another way to do that.

    You can use DynamicTableEntity which is in Microsoft.WindowsAzure.Storage.Table Namespace put all your properties in DynamicTableEntity.Properties property.

    Please see my simple code

    protected void Page_Load(object sender, EventArgs e)
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        ConfigurationManager.AppSettings["StorageConnectionString"]);
    
                var tableClient = storageAccount.CreateCloudTableClient();
          
                CloudTable table=tableClient.GetTableReference("Logs");
                var query = new TableQuery();
                var TableResults = table.ExecuteQuery(query);
                DataTable propertiesTable = new DataTable("LogTable");
                DataColumn partitionKeyColumn = new DataColumn();
                partitionKeyColumn.DataType = System.Type.GetType("System.String");
                partitionKeyColumn.ColumnName = "Partition Key";
                propertiesTable.Columns.Add(partitionKeyColumn);
    
                foreach (var result in TableResults)
    	        {
                    DataRow row;
                    row = propertiesTable.NewRow();
                    row["Partition Key"] = result.PartitionKey;
                    if (result.Properties!=null)
                    {
                        foreach (var kvp in result.Properties)
                        {
                            //Add column if not exsit first
                            if (!propertiesTable.Columns.Contains(kvp.Key))
                            {
                                DataColumn Column = new DataColumn();
                                Column.ColumnName = kvp.Key;
                                Column.DataType = System.Type.GetType("System.String");
                                propertiesTable.Columns.Add(Column);
                            }
                            //TODO: Should not use StringValue directly, Should create a method get kvp's string value.
                            //This only work for String Type will cause error in other type.
                            row[kvp.Key] = kvp.Value.StringValue;
                        }
                    }
                    propertiesTable.Rows.Add(row);
    	        }
                GridView1.DataSource = propertiesTable;
                GridView1.DataBind();
          
            }


    Please mark post as answered if it helped!



    Tuesday, August 13, 2013 2:15 AM
  • There is a nice discussion on this at http://stackoverflow.com/questions/10697752/how-do-i-retrieve-multiple-types-of-entities-using-a-single-query-to-azure-table.

    Especially the considerations around exploring the options to simplify this and what kind of queries you want to run against these entities are interesting, if you haven't already done so.

    HTH

    Tuesday, August 13, 2013 3:55 AM
  • HI

    I update my answer, Hope it helpful.

    Thanks.


    Please mark post as answered if it helped!

    Monday, August 19, 2013 8:29 AM