When using SQLBulkCopy on a table with a GUID primary key and default newsequentialid()
e.g
CREATE TABLE [dbo].[MyTable](
[MyPrimaryKey] [uniqueidentifier] NOT NULL CONSTRAINT [MyConstraint] DEFAULT (newsequentialid()),
[Status] [int] NULL,
[Priority] [int] NULL,
CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED
(
[MyPrimaryKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
wIth the C# code
tran = connection.BeginTransaction();
SqlBulkCopy sqlCopy = new SqlBulkCopy(connection,SqlBulkCopyOptions.Default, tran);
sqlCopy.DestinationTableName = "MyTable";
sqlCopy.WriteToServer(dataTable);
Gives you an error...
Column 'MyPrimaryKey' does not allow DBNull.Value
I've tried fiddling the the SqlBulkCopyOptions. The only thing that works is setting the MyPrimaryKey field to allow nulls and removing the primary key.
Anyone know if there is a workaround for this issue? Or can you verify that there is no workaround (other than changing the table structure)?