SqlMetadataStore.InitializeReplicaMetadata 方法
在中繼資料存放區內建立及初始化複寫的中繼資料,並傳回用來存取複寫中繼資料的複寫中繼資料物件。
命名空間: Microsoft.Synchronization.MetadataStorage
組件: Microsoft.Synchronization.MetadataStorage (在 microsoft.synchronization.metadatastorage.dll)
語法
'宣告
Public Overrides Function InitializeReplicaMetadata ( _
idFormats As SyncIdFormatGroup, _
replicaId As SyncId, _
customItemFieldSchemas As IEnumerable(Of FieldSchema), _
customIndexedFieldSchemas As IEnumerable(Of IndexSchema) _
) As ReplicaMetadata
'用途
Dim instance As SqlMetadataStore
Dim idFormats As SyncIdFormatGroup
Dim replicaId As SyncId
Dim customItemFieldSchemas As IEnumerable(Of FieldSchema)
Dim customIndexedFieldSchemas As IEnumerable(Of IndexSchema)
Dim returnValue As ReplicaMetadata
returnValue = instance.InitializeReplicaMetadata(idFormats, replicaId, customItemFieldSchemas, customIndexedFieldSchemas)
public override ReplicaMetadata InitializeReplicaMetadata (
SyncIdFormatGroup idFormats,
SyncId replicaId,
IEnumerable<FieldSchema> customItemFieldSchemas,
IEnumerable<IndexSchema> customIndexedFieldSchemas
)
public:
virtual ReplicaMetadata^ InitializeReplicaMetadata (
SyncIdFormatGroup^ idFormats,
SyncId^ replicaId,
IEnumerable<FieldSchema^>^ customItemFieldSchemas,
IEnumerable<IndexSchema^>^ customIndexedFieldSchemas
) override
public ReplicaMetadata InitializeReplicaMetadata (
SyncIdFormatGroup idFormats,
SyncId replicaId,
IEnumerable<FieldSchema> customItemFieldSchemas,
IEnumerable<IndexSchema> customIndexedFieldSchemas
)
public override function InitializeReplicaMetadata (
idFormats : SyncIdFormatGroup,
replicaId : SyncId,
customItemFieldSchemas : IEnumerable<FieldSchema>,
customIndexedFieldSchemas : IEnumerable<IndexSchema>
) : ReplicaMetadata
參數
- idFormats
提供者 (Provider) 的識別碼格式結構描述。
- replicaId
與這個中繼資料相關聯的複寫識別碼。
- customItemFieldSchemas
每一個中繼資料項目之自訂中繼資料欄位的結構描述資訊集合。如果沒有任何自訂中繼資料欄位,可能為 null 參考 (在 Visual Basic 中為 Nothing)。
- customIndexedFieldSchemas
索引結構描述清單,可以用於更有效率地尋找中繼資料存放區內的項目。如果沒有任何自訂索引,就可以是 null 參考 (在 Visual Basic 中為 Nothing)。
傳回值
複寫中繼資料物件,可以用於存取中繼資料存放區內的複寫中繼資料。
例外
例外狀況型別 | 條件 |
---|---|
這個物件已被處置,或是未正確初始化。 |
|
idFormats 或 replicaId 是 null 參考 (在 Visual Basic 中為 Nothing)。 |
|
customItemFieldSchemas 中任何自訂欄位名稱的長度為 0。 |
|
customItemFieldSchemas 中任何自訂欄位名稱為 null 參考 (在 Visual Basic 中為 Nothing)。 |
|
customIndexedFieldSchemas 中列出的欄位不存在於 customItemFieldSchemas 中。 |
|
replicaId 的格式不符合 idFormats 中指定的格式。 |
|
idFormats 指定之識別碼的長度超過 8000 個位元組。 |
|
尚未開啟或建立中繼資料存放區。 |
|
複寫中繼資料已經存在於指定之 replicaId 的中繼資料存放區內。 |
備註
這個方法是用來在中繼資料存放區內建立一組新的複寫中繼資料。若要在中繼資料存放區內存取現有的複寫中繼資料,請使用 GetReplicaMetadata。
這個方法會傳回中繼資料儲存服務所提供之 ReplicaMetadata 抽象類別的實作。這個抽象類別可以用於存取 Sync Framework 資料庫檔案中所儲存的複寫中繼資料。
使用 customItemFieldSchemas 可對項目中繼資料定義一組自訂欄位,每一個欄位都是由唯一的字串名稱和值所組成。這些欄位可存放有關預設項目中繼資料集合所不支援之項目的任何其他中繼資料。這些欄位可透過 ItemMetadata 上的各種方法 (如 ItemMetadata.GetByteField 或 Microsoft.Synchronization.MetadataStorage.ItemMetadata.SetCustomField) 來存取。
使用 customIndexedFieldSchemas 可定義一組索引結構描述,這樣就可以使用自訂欄位集合當做索引,更有效率地尋找中繼資料存放區內的項目。索引結構描述可定義為唯一的項目,以確保此索引會定義一個項目。索引結構描述內所包含的每一個欄位也必須存在於為此複寫所定義的自訂欄位結構描述中。索引欄位可用於 ReplicaMetadata.FindItemMetadataByIndexedField 和 ReplicaMetadata.FindItemMetadataByUniqueIndexedFields 等方法中。
範例
當複寫中繼資料尚未存在時,下列範例會初始化 SqlMetadataStore 物件中的複寫中繼資料。此範例會在複寫中繼資料內指定一組自訂欄位,同時指定將這些自訂欄位當做唯一索引使用。
public void NewStore(string StoreName, MetadataStore metaStore, bool metaStoreIsNew)
{
// Close the current store. This is necessary to release the ReplicaMetadata object held by this object.
Close(true);
// Keep the store name for later use.
_StoreName = StoreName;
// The absolute path of the item store is used as the replica ID.
string StoreAbsPath = Path.GetFullPath(StoreName);
// Get or initialize replica metadata in the metadata store.
_ContactMetadataStore = metaStore;
if (!metaStoreIsNew)
{
// The metadata store exists, so open it and get the replica metadata for the current replica.
// The replica ID is the absolute path of the item store.
_ContactReplicaMetadata = _ContactMetadataStore.GetReplicaMetadata(ContactIdFormatGroup,
new SyncId(StoreAbsPath));
// Read the contacts from the item store and the metadata store and save them in two
// in-memory lists. These lists are modified in memory by the methods in this object
// and committed to the disk when SaveChanges is called.
StreamReader contactReader;
contactReader = File.OpenText(StoreName);
Contact contact = ReadNextContact(contactReader);
while (null != contact)
{
ItemMetadata itemMeta = FindMetadata(contact);
_ContactList.Add(itemMeta.GlobalId, contact);
_ContactItemMetaList.Add(itemMeta.GlobalId, itemMeta);
contact = ReadNextContact(contactReader);
}
contactReader.Close();
}
else
{
// The metadata store does not exist, so create a new one.
// Create custom fields for First Name, Last Name, and Phone Number. These will be used
// as unique index fields for identifying items between the metadata store and the item store.
FieldSchema[] CustomFields =
{
new FieldSchema(FirstNameField, typeof(string), 100),
new FieldSchema(LastNameField, typeof(string), 100),
new FieldSchema(PhoneNumberField, typeof(string), 20)
};
// Specify the custom fields as a unique index.
string[] IndexFields = { FirstNameField, LastNameField, PhoneNumberField };
IndexSchema[] Indexes =
{
new IndexSchema(IndexFields, true)
};
// Create the metadata for the replica in the metadata store.
_ContactReplicaMetadata = _ContactMetadataStore.InitializeReplicaMetadata(
ContactIdFormatGroup, new SyncId(StoreAbsPath), CustomFields, Indexes);
// Set the provider version
_ContactReplicaMetadata.ProviderVersion = (uint)ContactsProviderVersion.ContactsProvider_v1;
}
}
請參閱
參考
SqlMetadataStore 類別
SqlMetadataStore 成員
Microsoft.Synchronization.MetadataStorage 命名空間