C# dataRow[“id”]=…等。我不断收到主键重复冲突。它不应与表定义中的一样[id][int]标识(1,1)不为空,是主键并自动递增1。所以它应该遵守规则。 SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOption

C# dataRow[“id”]=…等。我不断收到主键重复冲突。它不应与表定义中的一样[id][int]标识(1,1)不为空,是主键并自动递增1。所以它应该遵守规则。 SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOption,c#,sql-server,sqlbulkcopy,identity-insert,C#,Sql Server,Sqlbulkcopy,Identity Insert,dataRow[“id”]=…等。我不断收到主键重复冲突。它不应与表定义中的一样[id][int]标识(1,1)不为空,是主键并自动递增1。所以它应该遵守规则。 SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOptions.KeepIdentity); sbc.DestinationTableName = LOOKUP_TABLE; private static SqlBulkCopy GetBulkCopy(SqlBulkCopyOptions optio


dataRow[“id”]=…等。
我不断收到主键重复冲突。它不应与表定义中的一样[id][int]标识(1,1)不为空,是主键并自动递增1。所以它应该遵守规则。
SqlBulkCopy sbc = GetBulkCopy(SqlBulkCopyOptions.KeepIdentity);
sbc.DestinationTableName = LOOKUP_TABLE;

private static SqlBulkCopy GetBulkCopy(SqlBulkCopyOptions options = 
    SqlBulkCopyOptions.Default) 
{
    Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/RSWifi");
    string connString =
    cfg.ConnectionStrings.ConnectionStrings["WifiData"].ConnectionString;
    return new SqlBulkCopy(connString, options);
}
dt.Columns.Cast<DataColumn>().ForEach((c, i) => sqlBulkCopy.ColumnMappings.Add(c.ColumnName, i + 1));
CREATE TABLE [dbo].[ProductShippingMethodMap](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NOT NULL,
    [ShippingMethodId] [int] NOT NULL,
    [ParentProductId] [int] NOT NULL,
 CONSTRAINT [PK_ProductShippingMethodMap] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
 DataTable dtQtyData = new DataTable();
        dtQtyData.Clear();
        dtQtyData.Columns.Add("Id", typeof(int));

    dtQtyData.Columns.Add("ProductId", typeof(int));
    dtQtyData.Columns.Add("ShippingMethodId", typeof(int));
    dtQtyData.Columns.Add("ParentProductId", typeof(int));


    for (int i = 0; i < ShippingMethodIds.Length; i++)
    {
        for (int j = 0; j < ProductIds.Length; j++)
        {
            var productId = ProductIds[j];
            var shippingMethodId = ShippingMethodIds[i];
            dtQtyData.Rows.Add(new object[] {0,productId, shippingMethodId, parentProductId });
        }

    }
    var connectionString = new DataSettingsManager().LoadSettings().DataConnectionString;
    SqlBulkCopy bulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.Default);
    bulkcopy.DestinationTableName = "ProductShippingMethodMap";
    bulkcopy.WriteToServer(dtQtyData);