如何从 TSqlObject 实例中获取主键列?

How to get primary key columns out of a TSqlObject instance?

我试图通过以下方式从 TSqlObject 实例中获取主键列:

var constraint = table.GetReferenced(ModelSchema.PrimaryKeyConstraint, DacQueryScopes.All);

当然这行不通,因为 GetReferenced 方法需要一个 ModelRelationshipClass 实例的实例。

那怎么办呢?

在给定 table 的情况下获取 PK 约束的代码看起来有点像这样:

private static TSqlObject GetPrimaryKeyConstraint(TSqlObject table)
{
    IEnumerable<TSqlObject> constraints = table.GetReferencing(PrimaryKeyConstraint.Host, DacQueryScopes.UserDefined);
    return constraints.First();
}

也看看这个:

https://github.com/Microsoft/DACExtensions/tree/master/DacFxStronglyTypedModel

不用枚举/关系模型也能得到你想要的:)