Generator 类

表示数据生成器的抽象基类。

继承层次结构

System.Object
  Microsoft.Data.Schema.Tools.DataGenerator.Generator

命名空间:  Microsoft.Data.Schema.Tools.DataGenerator
程序集:  Microsoft.Data.Schema.Tools(在 Microsoft.Data.Schema.Tools.dll 中)

语法

声明
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator _
    Implements IGenerator, IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public ref class Generator abstract : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[<AbstractClass>]
[<GeneratorAttribute(typeof(DefaultGeneratorDesigner))>]
[<CLSCompliantAttribute(true)>]
type Generator =  
    class
        interface IGenerator
        interface IDisposable
        interface IExtensionInformation
        interface IExtension
    end
public abstract class Generator implements IGenerator, IDisposable, IExtensionInformation, IExtension

Generator 类型公开以下成员。

构造函数

  名称 说明
受保护的方法 Generator 从派生类中的构造函数中调用,用于初始化 Generator 类。

页首

属性

  名称 说明
公共属性 Collation 获取或设置此列的排序规则(即按照语言学进行相应排序)。
公共属性 Column 获取或设置列。
公共属性 ColumnName 获取或设置列名。
公共属性 Constraints 获取或设置列的 CHECK 约束信息。
公共属性 DefaultLocaleId 获取或设置区域设置 ID。
公共属性 DefaultValue 获取或设置列的默认值。
公共属性 ExtensionHandle 获取扩展句柄。
公共属性 MaxLength 获取或设置此列中值的最大长度。
公共属性 Nullable 获取或设置一个值,指示是否可将此列设置为 nullnull 引用(在 Visual Basic 中为 Nothing)。
公共属性 OutputKey 获取或设置用于检索输出值的键。
公共属性 PercentageNull 获取或设置此列中用户希望为 nullnull 引用(在 Visual Basic 中为 Nothing) 的生成值所占的百分比。此百分比表示为一个介于 0 和 100 之间的整数。
公共属性 Precision 获取或设置数据的精度。
公共属性 RowsToInsert 获取或设置要生成的行数。
公共属性 Scale 获取或设置数据的小数位数。
公共属性 SchemaName 获取或设置列所属架构的名称。
公共属性 Seed 获取或设置种子。可以使用种子初始化确定性随机数据生成算法。
公共属性 TableName 获取或设置列所属表的名称。
公共属性 TargetType 获取或设置列的目标数据类型。
公共属性 Unique 获取或设置一个值,该值指示所生成的列是否为唯一约束或主键约束的一部分。

页首

方法

  名称 说明
公共方法 Dispose() 释放由 Generator 使用的所有资源。
受保护的方法 Dispose(Boolean) 释放由 Generator 使用的非托管资源,还可以选择释放托管资源。
公共方法 Equals 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GenerateNextValues 根据数据生成算法生成下一个值。
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetOutputValue 通过使用 OutputKey 检索输出值。
公共方法 GetType 获取当前实例的 Type。 (继承自 Object。)
公共方法 Initialize 初始化数据生成器。
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
受保护的方法 OnGenerateNextValues 根据数据生成算法生成下一个值。
受保护的方法 OnGetOutputValue 检索由一个或多个值组成的当前结果集。
受保护的方法 OnInitialize 初始化生成器。
受保护的方法 OnNextValues 为数据行生成下一组值。
受保护的方法 OnSetInputValues 使用所提供的指定列表设置数据生成器的输入属性。
受保护的方法 OnValidateInputs 验证是否已设置了数据生成器所需的全部输入。
公共方法 SetExtensionHandle 使用指定的扩展句柄设置扩展句柄。
公共方法 SetInputValues 为数据生成器设置输入值。
公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。)
公共方法 ValidateInputs 验证是否已设置了数据生成器所需的全部输入。

页首

备注

如果标准数据生成器不能满足需要,您可以创建自定义数据生成器。 若要创建自定义数据生成器,必须创建一个实现 IGenerator 的类或从 Generator 继承的类。 您可以通过使用 GeneratorAttribute 修饰该类来将其标识为数据生成器。

您可以为自定义数据生成器创建自定义设计器,也可以使用 DefaultGeneratorDesigner

基类实现构造基于标记为 OutputAttribute 的公共属性的输出。 输入是使用 InputAttribute 设置的。 使用通过特性标记的属性可提供一个用于声明强类型输入和输出值的简单机制。

示例

标准数据生成器无法生成满足某些 check 约束的数据。 例如,如果 check 约束要求日期位于两个不同范围中的任一范围内,则不能使用标准 DateTime 生成器。 此示例创建一个能够满足类似约束的自定义数据生成器。 该生成器接受两个不同的范围作为其输入,并生成一个位于其中一个范围内的随机日期。 有关更多信息,请参见演练:为 CHECK 约束创建自定义数据生成器

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;

namespace GeneratorDateRangesCS
{
    public class GeneratorDateRangesCS : Generator
    {
        DateTime mRange1Min;
        DateTime mRange1Max;

        DateTime mRange2Min;
        DateTime mRange2Max;

        [Input]
        public DateTime Range1Min
        {
            set {mRange1Min = value;}
        }

        [Input]
        public DateTime Range1Max
        {
            set {mRange1Max = value;}
        }

        [Input]
        public DateTime Range2Min
        {
            set {mRange2Min = value;}
        }

        [Input]
        public DateTime Range2Max
        {
            set {mRange2Max = value;}
        }

        DateTime mRandomDate;

        [Output]
        public DateTime RandomDate
        {
            get {return mRandomDate;}
        }

        Random mRandom;
        Random mRandomRange;

        protected override void OnInitialize(GeneratorInit initInfo)
        {
            // Deterministic
            mRandom = new Random(this.Seed);
            mRandomRange = new Random(this.Seed);  //deterministic

            // Non-deterministic
            //mRandom = new Random();
            //mRandomRange = new Random();

            base.OnInitialize(initInfo);
        }

        protected override void OnGenerateNextValues()
        {
            DateTime min;
            DateTime max;

            // Generate a random date from either range 1 or range 2.
            // Randomly select either range 1 or range 2 by randomly 
            // generating an odd or an even random number.
            if (mRandomRange.Next() % 2 == 0)  //check for odd or even
            {
                min = mRange1Min;
                max = mRange1Max;
            }
            else
            {
                min = mRange2Min;
                max = mRange2Max;
            }

            // The formula for creating a random number in a specific
            // range is: of range + (size of range * random number 
            // between 0 and 1)

            //size of range
            TimeSpan range = max - min;

            // (Size of range * random number between 0 and 1)
            TimeSpan randomNumber = new TimeSpan(
                (long)(range.Ticks * mRandom.NextDouble()));

            // Start of range + (size of range * random number 
            // between 0 and 1)
            mRandomDate = min + randomNumber;
        }
    } // End of class
}     // End of namespace
Imports Microsoft.VisualStudio.TeamSystem.Data.DataGenerator

Public Class GeneratorDateRangesVB
    Inherits Generator

    Dim mRange1Min As Date
    Dim mRange1Max As Date

    Dim mRange2Min As Date
    Dim mRange2Max As Date

    <Input()> _
    Public WriteOnly Property Range1Min() As Date
        Set(ByVal value As Date)
            mRange1Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range1Max() As Date
        Set(ByVal value As Date)
            mRange1Max = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Min() As Date
        Set(ByVal value As Date)
            mRange2Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Max() As Date
        Set(ByVal value As Date)
            mRange2Max = value
        End Set
    End Property

    Dim mRandomDate As Date

    <Output()> _
    Public ReadOnly Property RandomDate() As Date
        Get
            Return mRandomDate
        End Get
    End Property

    Dim mRandom As Random
    Dim mRandomRange As Random

    Protected Overrides Sub OnInitialize(ByVal initInfo _
        As GeneratorInit)

        mRandom = New Random(Me.Seed)       'deterministic
        mRandomRange = New Random(Me.Seed)  'deterministic

        'mRandom = New Random()              'non-deterministic
        'mRandomRange = New Random()         'non-deterministic

        MyBase.OnInitialize(initInfo)
    End Sub

    Protected Overrides Sub OnGenerateNextValues()
        Dim min As Date
        Dim max As Date

        ' Generate a random date from either range 1 or range 2.
        ' Randomly select either range 1 or range 2 by randomly 
        ' generating an odd or an even random number.
        '------------------------------------------------------------
        If mRandomRange.Next() Mod 2 = 0 Then  'check for odd or even
            min = mRange1Min
            max = mRange1Max
        Else
            min = mRange2Min
            max = mRange2Max
        End If

        ' The formula for creating a random number in a specific 
        ' range is: of range + (size of range * random number 
        ' between 0 and 1)

        ' Size of range
        Dim range As TimeSpan = max - min

        ' (Size of range * random number between 0 and 1)
        Dim randomNumber As TimeSpan = _
            New TimeSpan(CLng(range.Ticks * mRandom.NextDouble()))

        ' Start of range + (size of range * random number 
        ' between 0 and 1)
        mRandomDate = min + randomNumber
    End Sub
End Class

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

请参见

参考

Microsoft.Data.Schema.Tools.DataGenerator 命名空间

GeneratorAttribute

GeneratorInit

IGenerator

其他资源

演练:为 CHECK 约束创建自定义数据生成器

Creating Custom Data Generators

数据生成器扩展性概述

Specifying Details of Data Generation for a Column