共用方式為


SqlMetadataStore.GetReplicaMetadata 方法

取得複寫中繼資料物件,用於存取中繼資料存放區中的複寫中繼資料。

命名空間: Microsoft.Synchronization.MetadataStorage
組件: Microsoft.Synchronization.MetadataStorage (在 microsoft.synchronization.metadatastorage.dll)

語法

'宣告
Public Overrides Function GetReplicaMetadata ( _
    idFormats As SyncIdFormatGroup, _
    replicaId As SyncId _
) As ReplicaMetadata
'用途
Dim instance As SqlMetadataStore
Dim idFormats As SyncIdFormatGroup
Dim replicaId As SyncId
Dim returnValue As ReplicaMetadata

returnValue = instance.GetReplicaMetadata(idFormats, replicaId)
public override ReplicaMetadata GetReplicaMetadata (
    SyncIdFormatGroup idFormats,
    SyncId replicaId
)
public:
virtual ReplicaMetadata^ GetReplicaMetadata (
    SyncIdFormatGroup^ idFormats, 
    SyncId^ replicaId
) override
public ReplicaMetadata GetReplicaMetadata (
    SyncIdFormatGroup idFormats, 
    SyncId replicaId
)
public override function GetReplicaMetadata (
    idFormats : SyncIdFormatGroup, 
    replicaId : SyncId
) : ReplicaMetadata

參數

  • idFormats
    提供者 (Provider) 的識別碼格式結構描述。
  • replicaId
    與這個中繼資料相關聯之複寫的識別碼。

傳回值

用來存取中繼資料存放區內之複寫中繼資料的複寫中繼資料物件。

例外

例外狀況型別 條件

ObjectDisposedException

這個物件已被處置,或是未正確初始化。

ArgumentNullException

idFormats 或 replicaId 是 null 參考 (在 Visual Basic 中為 Nothing)。

SyncIdFormatMismatchException

replicaId 的格式與 idFormats 中所指定的格式不相符,或是 idFormats 不符合初始化複寫中繼資料時所使用的識別碼格式結構描述。

ArgumentOutOfRangeException

idFormats 指定之識別碼的長度超過 8000 個位元組。

InvalidOperationException

尚未開啟或建立中繼資料存放區。

ReplicaMetadataInUseException

此複寫中繼資料物件的執行個體已經為使用中。

ReplicaMetadataNotFoundException

找不到識別碼為 replicaId 的複寫中繼資料。

備註

這個方法用於存取已經存在於中繼資料存放區內的複寫中繼資料。若要在中繼資料存放區內建立新的複寫中繼資料,請使用 InitializeReplicaMetadata

這個方法會傳回中繼資料儲存服務所提供之 ReplicaMetadata 抽象類別的實作。這個抽象類別可以用於存取 Sync Framework 資料庫檔案中所儲存的複寫中繼資料。

為了避免應用程式對中繼資料存放區進行同時的衝突更新,不允許特定複寫識別碼有多個待處理的 ReplicaMetadata 執行個體。應用程式可以從多個執行緒存取相同的 ReplicaMetadata 物件,但是不能有多個處理序同時存取複寫中繼資料。如果特定複寫識別碼已經有待處理的 ReplicaMetadata 執行個體存在,這個方法會擲回 ReplicaMetadataInUseException

範例

當複寫中繼資料已經存在時,下列範例會取得 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 命名空間