我正在使用MySqlBulkLoader
将文件批量插入到具有外键约束的表中。
using (var conn = new MySqlConnection(connectionString))
{
var bl = new MySqlBulkLoader(conn);
bl.TableName = tableName;
bl.Timeout = 600;
bl.FieldTerminator = ",";
bl.LineTerminator = "\r\n";
bl.FileName = tempFilePath;
bl.NumberOfLinesToSkip = 1;
numberOfInsertedRows = bl.Load();
}
文件内容如下所示:
CampaignRunId,RecipientId,IsControlGroup
27,"testrecipient_0",False
27,"testrecipient_1",False
27,"testrecipient_2",False
27,"testrecipient_3",False
27,"testrecipient_4",False
27,"testrecipient_5",False
27,"testrecipient_6",False
27,"testrecipient_7",False
27,"testrecipient_8",False
27,"testrecipient_9",False
27,"testrecipient_10",False
27,"testrecipient_11",False
27,"testrecipient_12",False
27,"testrecipient_13",False
CampaignRunId
列是引用另一个表CampaignRun
的Id列的FK。 id 27
已存在于该表中,当我在MySql Workbench上直接使用单个INSERT查询时,插入成功。但是从我的程序中,这似乎不起作用,我得到以下MySqlException
:
无法添加或更新子行:外键约束失败 (
postbag
。campaignrecipient
,CONSTRAINTFK_CampaignRecipient_CampaignRun
外键(CampaignRunId
) 参考campaignrun
(Id
)ON UPETE CASCADE ON UPDATE CASCADE)
我应该在插入之前删除外键并在之后添加它们吗?