Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public interface ITempTableBulkInsertOptions
/// Gets properties to insert.
/// </summary>
IEntityPropertiesProvider? PropertiesToInsert { get; }

/// <summary>
/// For temptable creation we should not use defaults
/// </summary>
bool DoNotUseDefaultValues { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public interface ITempTableCreationOptions
/// Set <see cref="TruncateTableIfExists"/> to <c>true</c> on re-use.
/// </remarks>
bool DropTableOnDispose { get; }

/// <summary>
/// For temptable creation we should not use defaults
/// </summary>
bool DoNotUseDefaultValues { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public IPrimaryKeyPropertiesProvider PrimaryKeyCreation
/// <inheritdoc />
public bool DropTableOnDispose { get; set; }

/// <inheritdoc />
public bool DoNotUseDefaultValues { get; set; }

/// <summary>
/// Initializes a new instance of <see cref="TempTableCreationOptions"/>.
/// </summary>
Expand All @@ -46,6 +49,7 @@ public TempTableCreationOptions(ITempTableCreationOptions? options = null)
if (options is null)
{
DropTableOnDispose = true;
DoNotUseDefaultValues = true;
}
else
{
Expand All @@ -60,5 +64,6 @@ private void InitializeFrom(ITempTableCreationOptions options)
TruncateTableIfExists = options.TruncateTableIfExists;
PropertiesToInclude = options.PropertiesToInclude;
DropTableOnDispose = options.DropTableOnDispose;
DoNotUseDefaultValues = options.DoNotUseDefaultValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal SqlServerBulkOperationTempTableOptions(SqlServerBulkOperationTempTableO
MomentOfPrimaryKeyCreation = MomentOfSqlServerPrimaryKeyCreation.AfterBulkInsert;
DropTableOnDispose = true;
EnableStreaming = true;
DoNotUseDefaultValues = true;
}
else
{
Expand All @@ -31,6 +32,7 @@ internal SqlServerBulkOperationTempTableOptions(SqlServerBulkOperationTempTableO
BatchSize = options.BatchSize;
EnableStreaming = options.EnableStreaming;
UseDefaultDatabaseCollation = options.UseDefaultDatabaseCollation;
DoNotUseDefaultValues = options.DoNotUseDefaultValues;
}
}

Expand Down Expand Up @@ -94,6 +96,11 @@ internal SqlServerBulkOperationTempTableOptions(SqlServerBulkOperationTempTableO
/// </summary>
public bool UseDefaultDatabaseCollation { get; set; }

/// <summary>
/// Do not use default values
/// </summary>
public bool DoNotUseDefaultValues { get; set; }

internal void Populate(SqlServerTempTableBulkInsertOptions options)
{
options.BatchSize = BatchSize;
Expand All @@ -106,5 +113,6 @@ internal void Populate(SqlServerTempTableBulkInsertOptions options)
options.SqlBulkCopyOptions = SqlBulkCopyOptions;
options.UseDefaultDatabaseCollation = UseDefaultDatabaseCollation;
options.MomentOfPrimaryKeyCreation = MomentOfPrimaryKeyCreation;
options.DoNotUseDefaultValues = DoNotUseDefaultValues;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Data;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Options;
using Thinktecture.EntityFrameworkCore.TempTables;

namespace Thinktecture.EntityFrameworkCore.BulkOperations;
Expand Down Expand Up @@ -61,6 +62,10 @@ public abstract class SqlServerTempTableBulkOperationOptions : ITempTableBulkIns
/// </summary>
public AdvancedSqlServerTempTableBulkOperationOptions Advanced { get; }

/// <inheritdoc />
public bool DoNotUseDefaultValues { get; set; }


/// <summary>
/// Initializes new instance of <see cref="SqlServerTempTableBulkOperationOptions"/>.
/// </summary>
Expand All @@ -74,6 +79,7 @@ protected SqlServerTempTableBulkOperationOptions(ITempTableBulkInsertOptions? op
DropTableOnDispose = true;
MomentOfPrimaryKeyCreation = MomentOfSqlServerPrimaryKeyCreation.AfterBulkInsert;
EnableStreaming = true;
DoNotUseDefaultValues = true;
}
else
{
Expand All @@ -82,6 +88,7 @@ protected SqlServerTempTableBulkOperationOptions(ITempTableBulkInsertOptions? op
TableNameProvider = optionsToInitializeFrom.TableNameProvider;
PrimaryKeyCreation = optionsToInitializeFrom.PrimaryKeyCreation;
PropertiesToInsert = optionsToInitializeFrom.PropertiesToInsert;
DoNotUseDefaultValues = optionsToInitializeFrom.DoNotUseDefaultValues;

if (optionsToInitializeFrom is SqlServerTempTableBulkOperationOptions sqlServerOptions)
{
Expand All @@ -94,6 +101,7 @@ protected SqlServerTempTableBulkOperationOptions(ITempTableBulkInsertOptions? op
MomentOfPrimaryKeyCreation = sqlServerOptions.MomentOfPrimaryKeyCreation;

Advanced.UsePropertiesToInsertForTempTableCreation = sqlServerOptions.Advanced.UsePropertiesToInsertForTempTableCreation;
DoNotUseDefaultValues = sqlServerOptions.DoNotUseDefaultValues;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ private string GetColumnsDefinitions(SqlServerTempTableCreatorCacheKey options)

sb.Append(property.Property.IsNullable ? " NULL" : " NOT NULL");

if (options.DoNotUseDefaultValues)
{
isFirst = false;
continue;
}

if (IsIdentityColumn(property))
sb.Append(" IDENTITY");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public readonly struct SqlServerTempTableCreatorCacheKey
/// </summary>
public IReadOnlyCollection<PropertyWithNavigations> PrimaryKeys { get; }

/// <summary>
/// Do not use default values when we create a temp dable
/// </summary>
public bool DoNotUseDefaultValues { get; }

/// <summary>
/// Initializes new instance of <see cref="SqlServerTempTableCreatorCacheKey"/>.
/// </summary>
Expand All @@ -38,6 +43,7 @@ public SqlServerTempTableCreatorCacheKey(
UseDefaultDatabaseCollation = options.UseDefaultDatabaseCollation;
Properties = options.PropertiesToInclude.DeterminePropertiesForTempTable(entityType, true);
PrimaryKeys = options.PrimaryKeyCreation.GetPrimaryKeyProperties(entityType, Properties);
DoNotUseDefaultValues = options.DoNotUseDefaultValues;
}

/// <inheritdoc />
Expand All @@ -46,7 +52,9 @@ public bool Equals(SqlServerTempTableCreatorCacheKey other)
return TruncateTableIfExists == other.TruncateTableIfExists &&
UseDefaultDatabaseCollation == other.UseDefaultDatabaseCollation &&
Properties.AreEqual(other.Properties) &&
PrimaryKeys.AreEqual(other.PrimaryKeys);
PrimaryKeys.AreEqual(other.PrimaryKeys)
&& DoNotUseDefaultValues == other.DoNotUseDefaultValues
;
}

/// <inheritdoc />
Expand All @@ -65,6 +73,8 @@ public override int GetHashCode()
Properties.ComputeHashCode(hashCode);
PrimaryKeys.ComputeHashCode(hashCode);

hashCode.Add(DoNotUseDefaultValues);

return hashCode.ToHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public sealed class SqliteTempTableBulkInsertOptions : ITempTableBulkInsertOptio
/// </summary>
public AdvancedSqliteTempTableBulkInsertOptions Advanced { get; }

/// <inheritdoc />
public bool DoNotUseDefaultValues { get; set; }

/// <summary>
/// Initializes new instance of <see cref="SqliteTempTableBulkInsertOptions"/>.
/// </summary>
Expand Down