Share via


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
    提供程序的 ID 格式架构。
  • replicaId
    与此元数据相关联的副本的 ID。

返回值

用于访问元数据存储区中的副本元数据的副本元数据对象。

异常

异常类型 条件

ObjectDisposedException

此对象已释放或未正确初始化。

ArgumentNullException

idFormats 或 replicaId 为 null 引用(在 Visual Basic 中为 Nothing)。

SyncIdFormatMismatchException

replicaId 的格式与 idFormats 中指定的格式不匹配,或者 idFormats 与初始化副本元数据时使用的 ID 格式架构不匹配。

ArgumentOutOfRangeException

由 idFormats 指定的 ID 的长度超过 8000 个字节。

InvalidOperationException

尚未打开或创建元数据存储区。

ReplicaMetadataInUseException

此副本元数据对象的实例已为活动状态。

ReplicaMetadataNotFoundException

找不到具有 ID replicaId 的副本元数据。

备注

此方法用于访问元数据存储区中已存在的副本元数据。若要在元数据存储区中创建新的副本元数据,请使用 InitializeReplicaMetadata

此方法返回由元数据存储服务提供的 ReplicaMetadata 抽象类的实现。此抽象类可用于访问存储在 Sync Framework 数据库文件中的副本元数据。

为了阻止应用程序对元数据存储区进行并发冲突更新,不允许一个特定副本 ID 有多个未完成的 ReplicaMetadata 实例。应用程序可以从多个线程访问同一个 ReplicaMetadata 对象,但多个进程不能同时访问副本元数据。如果已存在一个特定副本 ID 的 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 命名空间