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
I am building an app that uses identity columns. I am finding that the identity columns work fine by themselves, and they work fine with a unique index on the identity column. However, if a PK is added (a unique constraint of type Primary Key), then BulkInsert produces the following error.
Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "Test_pKey"
DETAIL: Key (id)=(0) already exists.
at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Npgsql.NpgsqlBinaryImporter.Complete(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlBinaryImporter.Complete()
2
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.InsertAsync[T](DbContext context, IEnumerable1 entities, TableInfo tableInfo, Action1 progress, Boolean isAsync, CancellationToken cancellationToken)
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.Insert[T](DbContext context, Type type, IEnumerable1 entities, TableInfo tableInfo, Action1 progress)
at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, Type type, IEnumerable1 entities, TableInfo tableInfo, Action1 progress)
at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable1 entities, OperationType operationType, BulkConfig bulkConfig, Action1 progress)
at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IEnumerable1 entities, BulkConfig bulkConfig, Action1 progress, Type type)
at LookingGlass.System.lgImport.TestIdentityInsert() in /Volumes/LaCie-Data/LookingGlass/Code/LookingGlass.System/lgImport.cs:line 554
Exception data:
Severity: ERROR
SqlState: 23505
MessageText: duplicate key value violates unique constraint "Test_pKey"
Detail: Key (id)=(0) already exists.
Where: COPY Test, line 2
SchemaName: Finance
TableName: Test
ConstraintName: Test_pKey
File: nbtinsert.c
Line: 666
Routine: _bt_check_unique
For testing purposes, here is the definition of the 'Test' table.
DROP TABLE IF EXISTS "Finance"."Test";
CREATE TABLE IF NOT EXISTS "Finance"."Test"
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
val text COLLATE pg_catalog."default",
CONSTRAINT "Test_pKey" PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS "Finance"."Test"
OWNER to jloper;
COMMENT ON TABLE "Finance"."Test"
IS 'Testing Bulk Insert';
For testing purposes, here is the code that reproduces this issue. We never get to the success line. Always hits the catch/exception.
public void TestIdentityInsert()
{
using var context = new TestContext();
try
{
var itmLst = new List<Test>();
itmLst.Add(new Test() { val = "Foo1" });
itmLst.Add(new Test() { val = "Bar2" });
context.BulkInsert(itmLst);
Console.WriteLine("2 rows were inserted");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
The text was updated successfully, but these errors were encountered:
I am building an app that uses identity columns. I am finding that the identity columns work fine by themselves, and they work fine with a unique index on the identity column. However, if a PK is added (a unique constraint of type Primary Key), then BulkInsert produces the following error.
Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "Test_pKey"
DETAIL: Key (id)=(0) already exists.
at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder
1.StateMachineBox
1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)at Npgsql.NpgsqlBinaryImporter.Complete(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlBinaryImporter.Complete()
2
at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.InsertAsync[T](DbContext context, IEnumerable
1 entities, TableInfo tableInfo, Action
1 progress, Boolean isAsync, CancellationToken cancellationToken)at EFCore.BulkExtensions.SqlAdapters.PostgreSql.PostgreSqlAdapter.Insert[T](DbContext context, Type type, IEnumerable
1 entities, TableInfo tableInfo, Action
1 progress)at EFCore.BulkExtensions.SqlBulkOperation.Insert[T](DbContext context, Type type, IEnumerable
1 entities, TableInfo tableInfo, Action
1 progress)at EFCore.BulkExtensions.DbContextBulkTransaction.Execute[T](DbContext context, Type type, IEnumerable
1 entities, OperationType operationType, BulkConfig bulkConfig, Action
1 progress)at EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext context, IEnumerable
1 entities, BulkConfig bulkConfig, Action
1 progress, Type type)at LookingGlass.System.lgImport.TestIdentityInsert() in /Volumes/LaCie-Data/LookingGlass/Code/LookingGlass.System/lgImport.cs:line 554
Exception data:
Severity: ERROR
SqlState: 23505
MessageText: duplicate key value violates unique constraint "Test_pKey"
Detail: Key (id)=(0) already exists.
Where: COPY Test, line 2
SchemaName: Finance
TableName: Test
ConstraintName: Test_pKey
File: nbtinsert.c
Line: 666
Routine: _bt_check_unique
For testing purposes, here is the definition of the 'Test' table.
DROP TABLE IF EXISTS "Finance"."Test";
CREATE TABLE IF NOT EXISTS "Finance"."Test"
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
val text COLLATE pg_catalog."default",
CONSTRAINT "Test_pKey" PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS "Finance"."Test"
OWNER to jloper;
COMMENT ON TABLE "Finance"."Test"
IS 'Testing Bulk Insert';
For testing purposes, here is the code that reproduces this issue. We never get to the success line. Always hits the catch/exception.
The text was updated successfully, but these errors were encountered: