How to create a new Table using Table Storage in Azure
I have tried to use the samples that Roger Jennings recommeded in his book, "Cloud Computing with Windows Azure", but he is using version 1. I'm using v1.2 and there is a lot of differences. Firstly, I had to recompile the StorageClient DLL with the corrected namespace and other changes. Then, when I use his code to create a Table at the application start, I get an "out of range index".
Has anyone managed to succe开发者_StackOverflow中文版ssfully create a Table at application startup? If so, how? Also, if there are any tutorials/samples that use version 1.2, I'd greatly appreciate them too.
You no longer have to rebuild the sample storage client library. v1.2 will automagically add three DLL references to your role:
- Microsoft.WindowsAzure.Diagnostics
- Microsoft.WindowAzure.ServiceRuntime
- Microsoft.WindowsAzure.StorageClient
To create a table, you'll need to first set up your table :
- Create a class deriving from TableServiceEntity (say, "MyEntity")-
- Derive a table class from TableServiceContext (say, "MyEntityDataServiceContext"). In that class, create a property of type DataServiceQuery < MyEntity >() that returns CreateQuery < MyEntity > ("MyEntities");
Once you've done that, create the table with code like this:
var account = CloudStorageAccount.DevelopmentStorageAccount;
CloudTableClient.CreateTablesFromModel(typeof(MyEntityDataServiceContext),account.TableEndpoint.AbsoluteUri, account.Credentials);
For a much more detailed look at this, download the Azure Platform Training Kit. There's a lab called "Exploring Windows Azure Storage" that covers all this.
精彩评论