我有一个子表,它引用具有一对多关系的父表。 PK -> 多个外键。原因是 FK 表是与父表上的 ID 相关的对象列表。
如何使用 dapper 插入具有相同 FK 的多行?
assessmentModel.locationInformationModels = new List<LocationInformationModel>();
string sqlStatement = @"INSERT INTO LocationInformation
([LocationInformationId]
,[Address1]
,[Address2]
,[City]
,[State]
,[Zip])
VALUES(
@LocationInformationId,@Address1,@Address2,@City,@State,@Zip)";
using (var connection = new SqlConnection(_connectionString))
{
await connection.OpenAsync();
using (var ts = connection.BeginTransaction())
{
var retVal = await connection.ExecuteAsync(sqlStatement, assessmentModel.locationInformationModels, transaction:ts);
ts.Commit();
return retVal;
}
}