Search code examples
c#smo

SMO - PropertyNotSetException when accessing table properties


I'm not sure if this is intended behaviour since I can't find it documented anywhere on MSDN, but this still seems a little bit odd.

I'm writing a program that makes use of SMO. For the sake of testing. I've basically got a test database, with a table in it (the structure is unimportant) that has the following trigger.

CREATE TRIGGER [dbo].[MyTableInsert] ON [dbo].[MyTable] AFTER INSERT
AS
BEGIN
    PRINT 'after insert';
END

Now in my code. I expected to use this code to see (and prove) that the table 'MyTable' has an INSERT trigger. However running that code throws a PropertyNotSetException.

var table = new Table(database, tableName.ToString());
Console.WriteLine(table.HasInsertTrigger); // Throws PropertyNotSetException

But if I call the Refresh() method after initialising the table. The call to HasInsertTrigger returns true as excepted.

var table = new Table(database, tableName.ToString());
table.Refresh();
Console.WriteLine(table.HasInsertTrigger); // Returns true.

Making the call to Refresh seems unnecessary, but is it required as I can't find any documentation that says it needs to be called before accessing any properties.


Solution

  • I should have seen this a mile off... I was creating new instances of the tables rather than accessing existing ones.