如何从DACFx API获取时态表架构信息?

时间:2016-04-07 00:35:55

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

考虑以下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 -kind列,并获取有关表格的信息。主键。

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

我如何使用DACFx API了解这一点?

1 个答案:

答案 0 :(得分: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);