2

我正在使用以下方法在 Windows Azure 中创建表。此代码仅在应用程序首次初始化时执行一次

TableStorage.CreateTablesFromModel(typeof(customDataServiceContext), account);

如果我有要对表进行的更新(例如新列),我如何在不丢失现有数据的情况下进行这些更新?

4

2 回答 2

3

只需使用新字段更新用于表示实体的 CLR 类。表服务本身是无模式的,因此它唯一关心的是表的名称,即您的 CLR 类型的名称。

您无需再次调用 CreateTablesFromModel,因为该表已经存在。但是,如果您添加新表,您会这样做。您的旧表不会受此影响;已经存在的表被单独留下。

检索表中已有的实体将导致新列的值为空。

于 2009-07-28T13:33:28.180 回答
1

Rik correctly mentioned the most important concept: Azure Table Storage has no schema. As far as the storage system itself is concerned, you don't need to do anything if you want to change what you store in the entity set (table). Of course, if you've stored entities (rows) with different schemas, your business logic will need to be prepared to handle the differences. For example, you may need to handle a null value or even different data types depending the changes you've made.

It follows, then, that the system simply needs to know about the existence of an entity set (table) and doesn't care at all about it's contents. This is why there's no requirement to call .CreateTableFromModel when you decide to alter the schema of the entities you're storing.

In my opinion, the Azure folks confuse developers by using the term 'Table' when they really mean 'Entity Set'. Table leads developers down a familiar mental path with fixed schemas and columns. You really should think of an entity set a container of entities (property bags). The only enforeced requirement is that each entity must contain properties for PartitionKey, RowKey and ModifiedDate (?).

ONE FINAL IMPORTANT note is that everything I said is true for the actual Azure Table Storage facility in the cloud. The current implementation of developer storage is a local simulation of the cloud built with an actual SQL database. This means that, when using development storage, you will be restricted to actually use a fixed schema for your entity sets. This is a significant lacking in the development storage's simulation of the real thing, and it's a bummer.

于 2009-08-23T12:53:16.427 回答