【问题标题】:How can I get temporal table schema information from DACFx API?如何从 DACFx API 获取时态表模式信息?
【发布时间】:2016-04-07 00:35:55
【问题描述】:

考虑下面的 TSql 代码:

CREATE TABLE Employee 
(  
  [EmployeeID] int NOT NULL PRIMARY KEY CLUSTERED 
  , [Name] nvarchar(100) NOT NULL
  , [Position] varchar(100) NOT NULL 
  , [Department] varchar(100) NOT NULL
  , [Address] nvarchar(1024) NOT NULL
  , [AnnualSalary] decimal (10,2) NOT NULL
  , [ValidFrom] datetime2 (2) GENERATED ALWAYS AS ROW START
  , [ValidTo] datetime2 (2) GENERATED ALWAYS AS ROW END
  , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
 )  
 WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeeHistory));

...以及以下使用 DACFx 的 C# 代码:

TSqlObject table;
TSqlObject nameColumn;

// picture some code instantiating the two variables above

var primaryKey = table.GetChildren().Single(x => x.ObjectType == ModelSchema.PrimaryKeyConstraint);
var isMax = nameColumn.Object.GetProperty<bool?>(Column.IsMax);

所以在这里我们看到我已经查询了 DACFx API 以查明列实例是否是 MAX 类型的列,并获取有关表主键的信息。

现在我想知道表格的时间设置。我需要知道SYSTEM_VERSIONING 是否打开,如果打开,用作历史表的表是什么。我也想知道哪些字段被用作行的开始和结束。

我如何使用 DACFx API 知道这一点?

【问题讨论】:

    标签: c# .net sql-server dac temporal-database


    【解决方案1】:

    我知道这是一个老问题,但我花了一整天的时间终于弄清楚如何拉动 SYSTEM_VERSIONING 设置。希望这对其他人有帮助:

    TSqlObject table;
    
    // Code initializing the variable
    
    // Get the type of retention. This can be either -1 (if Infinite)
    // or one of the value in the TemporalRetentionPeriodUnit enum
    var retentionUnit = table.GetProperty<int>(Table.RetentionUnit);
    
    // Get the retention value. If retention is Infinite this will be -1
    var retentionValue = table.GetProperty<int>(Table.RetentionValue);
    
    // Get a reference to the history table. 
    var historyTable = table.GetReferenced(Table.TemporalSystemVersioningHistoryTable);
    

    【讨论】:

    • 如何将 int 转换为实际单位,即 TemporalRetentionPeriodUnit Days,Weeks 等
    • 我不需要转换它。但是您可以尝试使用表格,通过更改保留期并检查代码将为表格返回什么来查看每个数字对应的内容。
    • 最后我只需要从一个微软文档中复制值并对其进行硬编码
    猜你喜欢
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2012-03-03
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    相关资源
    最近更新 更多