You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When writing manual integration tests that require transient database objects, use the RAII classes from `Microsoft.Data.SqlClient.Tests.Common.Fixtures.DatabaseObjects` instead of manually writing `try/finally` blocks with DDL `DROP`/`CREATE` statements.
326
+
327
+
**Available classes:**
328
+
329
+
| Class | SQL generated | Example definition argument |
|`Table`|`CREATE TABLE {Name} {definition}`|`"(Id INT, Value NVARCHAR(100))"`|
332
+
|`StoredProcedure`|`CREATE PROCEDURE {Name} {definition}`|`"AS BEGIN SELECT 1 END"`|
333
+
|`UserDefinedType`|`CREATE TYPE [dbo].{Name} AS {definition}`|`"TABLE (f1 INT)"`|
334
+
335
+
Each class generates a unique object name from the given prefix (incorporating a timestamp-based GUID, username, and machine name), creates the object on construction (requiring the connection to already be open), and drops it when disposed. The generated name is available via the `.Name` property.
usingStoredProcedureproc=new(conn, "MyProc", $"ASBEGINSELECT * FROM {testTable.Name} END");
344
+
345
+
usingSqlCommandcmd=conn.CreateCommand();
346
+
cmd.CommandText=proc.Name;
347
+
cmd.CommandType=CommandType.StoredProcedure;
348
+
// ... objects are automatically dropped when the scope ends
349
+
```
350
+
351
+
**Rules:**
352
+
- Open the connection **before** constructing any database object (the constructor executes DDL immediately)
353
+
- When objects depend on each other (e.g., a stored procedure that references a table), declare the dependent object **last** so it is disposed first — `using` declarations are disposed in reverse order
354
+
- Use the `.Name` property directly wherever you need to reference the object in SQL; for `UserDefinedType` this already includes the `[dbo].` schema prefix, making it suitable for use as a TVP `TypeName`
0 commit comments