Search code examples
.netsql-serversql-server-data-toolsdacpacdac

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


I'm trying to get the primary key columns out of a TSqlObject instance in the following manner:

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

Of course this doesn't work, as the GetReferenced method expects an instance of ModelRelationshipClass instance.

So how can it be done?


Solution

  • Code to get the PK constraint given a table would look a bit like this:

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