Share via


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
    提供程序的 ID 格式架构。
  • replicaId
    与此元数据相关联的副本 ID。
  • customItemFieldSchemas
    每个元数据项的自定义元数据字段的架构信息集合。如果不存在自定义元数据字段,则可为 null 引用(在 Visual Basic 中为 Nothing)。
  • customIndexedFieldSchemas
    可用于更有效地查找元数据存储区中的项的索引架构列表。如果不存在自定义索引,则可以是 null 引用(在 Visual Basic 中为 Nothing)。

返回值

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

异常

异常类型 条件

ObjectDisposedException

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

ArgumentNullException

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

ArgumentException

customItemFieldSchemas 中的任一自定义字段名的长度为 0。

NullReferenceException

customItemFieldSchemas 中的任一自定义字段名为 null 引用(在 Visual Basic 中为 Nothing)。

MetadataFieldNotFoundException

customIndexedFieldSchemas 中列出的字段在 customItemFieldSchemas 中不存在。

SyncIdFormatMismatchException

replicaId 的格式与 idFormats 中指定的格式不匹配。

ArgumentOutOfRangeException

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

InvalidOperationException

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

ReplicaMetadataAlreadyExistsException

副本元数据已存在于指定 replicaId 的元数据存储区中。

备注

此方法用于在元数据存储区中创建一组新的副本元数据。若要访问元数据存储区中的现有副本元数据,请使用 GetReplicaMetadata

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

可以使用 customItemFieldSchemas 为项元数据定义一组自定义字段。每个字段均由一个唯一字符串名称和一个值组成。这些字段可用于存储该项的默认元数据组不支持的任何其他元数据。可通过对 ItemMetadata 使用各种方法来访问这些字段,如 ItemMetadata.GetByteFieldMicrosoft.Synchronization.MetadataStorage.ItemMetadata.SetCustomField

可以使用 customIndexedFieldSchemas 定义一组索引架构,这样就可以将自定义字段集用作索引,从而有效地在元数据存储区中查找项。索引架构可定义为唯一,以确保该索引定义单个项。包含在索引架构中的每个字段也还必须存在于为副本定义的自定义字段架构中。可以在 ReplicaMetadata.FindItemMetadataByIndexedFieldReplicaMetadata.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 命名空间