Need to get temp table name
mattjohnsonpint opened this issue · 2 comments
mattjohnsonpint commented
Hi, and thanks for putting this library together!
I'm using it to import data into a temp table, and then I need to follow that up with a SQL MERGE
statement. However, I need a way to get the name of the temp table so I can use it as the merge source. Here's what I'm currently doing:
await using var tempTable = await _dbContext.BulkInsertIntoTempTableAsync(tempData, bulkInsertOptions, cancellationToken);
string tempTableName = tempTable.GetTableName();
string mergeSql = $@"
MERGE RealTable AS dst
USING {tempTableName} AS src
ON dst.SomeValue = src.SomeValue
WHEN MATCHED THEN
...
WHEN NOT MATCHED THEN
...
;";
await _dbContext.Database.ExecuteSqlRawAsync(mergeSql, cancellationToken);
GetTableName
is implemented as an extension method that uses reflection:
public static string GetTableName<T>(this ITempTableQuery<T> tempTableQuery)
{
var fieldInfo = tempTableQuery.GetType().GetField("_tempTableReference", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo != null)
{
var tempTableReference = (ITempTableReference)fieldInfo.GetValue(tempTableQuery);
return tempTableReference.Name;
}
throw new Exception("Could not get temp table name!");
}
It would be better to not use reflection. Can you please expose the temp table name (or the reference) as a property on ITempTableQuery<T>
? Thanks.
PawelGerr commented
The name is exposed on ITempTableQuery<T>
.
Versions 1.4.0 (EF Core 2.x) and 2.0.0-beta005 (EF Core 3.1) should be on Nuget in a few minutes.
mattjohnsonpint commented
Works perfectly. Thanks!