Skip to content

Commit 3d8e3c8

Browse files
authored
Merge pull request #21 from poizan42/drop-table-if-exists-pre-sqlserver2016
Fix pre-SQL Server 2016 compatibility of "drop table if exists" when disposing temp tables.
2 parents ba6c537 + 3cbb107 commit 3d8e3c8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Thinktecture.EntityFrameworkCore.SqlServer/EntityFrameworkCore/TempTables/SqlServerTempTableReference.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public void Dispose()
5959
if (!_dropTableOnDispose || _database.GetDbConnection().State != ConnectionState.Open)
6060
return;
6161

62-
_database.ExecuteSqlRaw($"DROP TABLE IF EXISTS {_sqlGenerationHelper.DelimitIdentifier(Name)}");
62+
_database.ExecuteSqlRaw($@"
63+
IF(OBJECT_ID('tempdb..{Name}') IS NOT NULL)
64+
DROP TABLE {_sqlGenerationHelper.DelimitIdentifier(Name)};
65+
");
6366
_database.CloseConnection();
6467
}
6568
catch (ObjectDisposedException ex)

0 commit comments

Comments
 (0)