Here is my bulk insert function.
public async Task BulkInsertAsync(IEnumerable<T> entities)
{
await _context.BulkInsertAsync(entities,option => {
option.DisablePrimaryKeyNullCheck = true;
option.DisableSqlBulkCopyDirect = true;
});
}
Here is my Entity Framework batch insert function.
public async Task InsertAsync(IEnumerable<T> entities)
{
await _context.AddRangeAsync(entities);
await _context.SaveChangesAsync();
}
Batch insert works fine. but bulk insert function throws an exception:
{"ClassName":"System.TypeInitializationException",
"Message":"The type initializer for '?' threw an exception.",
"Data":null,
"InnerException": {"ClassName":"System.ArgumentNullException",
"Message":"Value cannot be null.",
"Data":null,
"InnerException":null, "HelpURL":null,
"StackTraceString":" at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method)\r\n at ?..cctor()","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2147467261,"Source":"System.Linq.Expressions","WatsonBuckets":"AQAAAEMATABSADIAMAByADMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","ParamName":"method"},"HelpURL":null,"StackTraceString":" at ?.BulkInsert[T](DbContext this, IEnumerable1 entities, Action
1 options, Boolean isBulkSaveChanges)\r\n at DbContextExtensions.BulkInsert[T](DbContext this, IEnumerable1 entities, Action
1 options)\r\n at DbContextExtensions.?1.?()\r\n at System.Threading.Tasks.Task.InnerInvoke()\r\n at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)\r\n at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n--- End of stack trace from previous location ---\r\n at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)\r\n--- End of stack trace from previous location ---\r\n at DbContextExtensions.BulkInsertAsync[T](DbContext this, IEnumerable
1 entities, Action1 options, CancellationToken cancellationToken)\r\n at Odw.Shared.Database.EfDataBase.Repositories.BaseRepository
1.BulkInsertAsync(IEnumerable`1 entities) in C:\Users\503325638\Code\ODW-Onboarding\odw-monitor\Odw\Shared\src\Database\EfDataBase\Repositories\BaseRepository.cs:line 84\r\n at Odw.Server.Monitor.AllMonitorRouter.InitDataAsync(String
tenant, Int32 days) in C:\Users\503325638\Code\ODW-Onboarding\odw-monitor\Odw\Server\src\Monitor\AllMonitorRouter.cs:line 96","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146233036,"Source":"Z.EntityFramework.Extensions.EFCore",
How to solve this problem?
CodePudding user response:
my Bad. entity framework extension doesn't throw the inner exception. the problem is that postgresql table column type is timestamp without time zone. i change to
timestamp(6) with time zone DEFAULT now();
in dotnet project Startup.cs
ConfigureServices(){
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}
All things was gone.